简体   繁体   中英

Reading double values in “Scanf” statement

I wrote a program for finding a root of a fifth degree polynomial, in a range that the user decides as an input. for example:

Please enter the coefficients of the polynomial: -64 0 0 0 0 2

Please enter the range:

4 -5.7

Invalid range! Please enter the range:

2 3.5

The polynomial has a root: x=2.

My problem is in when I enter for example the range **10.4 10.2" the program can't compare between two values and decide that its an invalid range. For integers it works.

How do I fix this?

#include <stdio.h>
#define ZERO 0.00001

int main()
{
    double a_0,a_1,a_2,a_3,a_4,a_5,end_of_range,beginning_of_range;
    int x,root;

    printf("Please enter the coefficients of the polynomial:\n");

    scanf("%lf%lf%lf%lf%lf%lf", &a_0, &a_1, &a_2, &a_3, &a_4, &a_5);

    printf("Please enter the range:\n");
    scanf("%lf%lf", &beginning_of_range, &end_of_range);
    while (beginning_of_range >= end_of_range)
    {
        printf("Invalid range! Please enter the range:\n");
        scanf("%lf%lf", &beginning_of_range, &end_of_range);
    }    
    x = beginning_of_range;    
    while (x <= end_of_range)
    {    
        if ((a_0 + a_1*x + a_2*x*x + a_3*x*x*x + a_4*x*x*x*x + a_5*x*x*x*x*x >= -ZERO) 
         && (a_0 + a_1*x + a_2*x*x + a_3*x*x*x + a_4*x*x*x*x + a_5*x*x*x*x*x <= ZERO))
        {
            root = x;
            printf("The polynomial has the root x=%d.", root);
            break;
        }

        x++;

        if( x > end_of_range)
        {
            printf("Could not find a root.");
            break;
        }
    }
    return 0;
}

Note: I want the roots to be only Integers! that's why I declared x as an int .

And something strange is happening, when I enter the range [10.4, 10.3] it just wait for about 1 minute and then prints "Could not find a root", although it must print invalid range.

Things to change:

  1. Change the type of x . Instead of

     int x; 

use

    double x;
  1. Declare root as a variable.

     double root; 
  2. Fix the format used to print root . Instead of

     printf("The polynomial has the root x=%d.", root); 

use

    printf("The polynomial has the root x=%lf.\n", root);

Make x a double , but let it only take on integer values.

// x = beginning_of_range;
x = ceil(beginning_of_range);

// while (x <= end_of_range)
while (x <= floor(end_of_range))

  // x++
  x += 1.0;

This approach will have trouble when |x| is large (about 1/DBL_EPSILON ) as x += 1.0; may not result in a incremented x .


BTW:

double y = ((((a_5*x + a_4)*x + a_3)*x + a_2)*x + a_1)*x + a_0; 
// is numerically more stable (and likely faster) than 
y = (a_0 + a_1*x + a_2*x*x + a_3*x*x*x + a_4*x*x*x*x + a_5*x*x*x*x*x;

Minor: Suggest removing = in range test. A range of [1 1] looks legitimate to me.

// while (beginning_of_range >= end_of_range)
while (beginning_of_range > end_of_range)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM