By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,346 Members | 2,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,346 IT Pros & Developers. It's quick & easy.

Why won't my code accept output for some values?

P: 1
I have this program that gets the roots of a polynomial. In the function to get the roots, I have a condition that asks if a certain array element is equal to zero. It works for some input like
2
1,4,4
but not on this one:
-2,-1,15
By the way, it also works with other values with a negative sign


Plus I see a negative zero in the output. Does it mean anything?

Here is the code: http://ideone.com/B4uCm1
I'm thinking that the problem is somewhere around here:
Expand|Select|Wrap|Line Numbers
  1. int  get_rational_roots_size(int polynomial_degree, double polynomial_coefficients[], double possible_roots[], int num_possible_roots)
  2. {
  3.     int i, j, num_rational_roots = 0;
  4.     double quotient_coefficients[polynomial_degree];
  5.  
  6.     quotient_coefficients[0] = polynomial_coefficients[polynomial_degree];
  7.     for(i = 0; i < num_possible_roots; i++)
  8.     {
  9.         for(j=1;j<=polynomial_degree;j++)
  10.         {
  11.             quotient_coefficients[j] = (quotient_coefficients[j-1]*possible_roots[i])+polynomial_coefficients[polynomial_degree-j];
  12.             if(quotient_coefficients[j] == +0 || quotient_coefficients[j] == -0 && j == 2)
  13.             {
  14.                 num_rational_roots++;
  15.             }
  16.         } 
  17.     }
  18.     return num_rational_roots;
  19. }
  20.  
  21. void get_rational_roots(int polynomial_degree, double polynomial_coefficients[], double possible_roots[], int num_possible_roots, double rational_roots[])
  22. {
  23.     int i, j, element = 0;
  24.     double quotient_coefficients[polynomial_degree];
  25.  
  26.     quotient_coefficients[0] = polynomial_coefficients[polynomial_degree];
  27.     for(i = 0; i < num_possible_roots; i++)
  28.     {
  29.         for(j=1;j<=polynomial_degree;j++)
  30.         {
  31.             quotient_coefficients[j] = (quotient_coefficients[j-1]*possible_roots[i])+polynomial_coefficients[polynomial_degree-j];
  32.             printf("Result %d = %lf\t", j, quotient_coefficients[j]);
  33.             if(quotient_coefficients[j] == +0 || quotient_coefficients[j] == -0 && j == 2)
  34.             {
  35.                 rational_roots[element] = possible_roots[i];
  36.                 printf("\nRoot %lf at i = %d, j = %d\n", rational_roots[element], i, j);
  37.                 element++;
  38.             }
  39.         }
  40.         printf("\n");
  41.     }
  42. }
  43.  
Ohh. And this is not a homework. Thanks :D
Oct 19 '15 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
I'd like to help but the code at http://ideone.com/B4uCm1 won't compile because it is using non-standard array definitions. That is, it s creating arrays on the stack using a non-const value for the number of elements.

Expand|Select|Wrap|Line Numbers
  1. double array[num_elements];
  2.  
instead of:

Expand|Select|Wrap|Line Numbers
  1. double* array = (double)malloc(num_elements * sizeof(double));
  2.  

Then there are various casts from double to int that generate warning about truncation and possible loss of data.

It's too much for me to fix.

You should read: http://bytes.com/topic/c/insights/77...rrays-revealed
Oct 19 '15 #2

Expert 100+
P: 2,400
+0.0 is the same as -0.0 for all but certain special conditions.
I don't know if your program involves any of those special cases. To find out, you need to change "+0" to "+0.0"; & similarly for -0.

+0 and -0 (integers) are always exactly the same.

FYI, 1.0/INFINITY is +0.0 while -1.0/INFINITY is -0.0. (INFINITY comes from math.h)

It is usually a bad idea to compare floating point values for equality (==) or inequality (!=).
Oct 20 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.