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
- int get_rational_roots_size(int polynomial_degree, double polynomial_coefficients[], double possible_roots[], int num_possible_roots)
- {
- int i, j, num_rational_roots = 0;
- double quotient_coefficients[polynomial_degree];
- quotient_coefficients[0] = polynomial_coefficients[polynomial_degree];
- for(i = 0; i < num_possible_roots; i++)
- {
- for(j=1;j<=polynomial_degree;j++)
- {
- quotient_coefficients[j] = (quotient_coefficients[j-1]*possible_roots[i])+polynomial_coefficients[polynomial_degree-j];
- if(quotient_coefficients[j] == +0 || quotient_coefficients[j] == -0 && j == 2)
- {
- num_rational_roots++;
- }
- }
- }
- return num_rational_roots;
- }
- void get_rational_roots(int polynomial_degree, double polynomial_coefficients[], double possible_roots[], int num_possible_roots, double rational_roots[])
- {
- int i, j, element = 0;
- double quotient_coefficients[polynomial_degree];
- quotient_coefficients[0] = polynomial_coefficients[polynomial_degree];
- for(i = 0; i < num_possible_roots; i++)
- {
- for(j=1;j<=polynomial_degree;j++)
- {
- quotient_coefficients[j] = (quotient_coefficients[j-1]*possible_roots[i])+polynomial_coefficients[polynomial_degree-j];
- printf("Result %d = %lf\t", j, quotient_coefficients[j]);
- if(quotient_coefficients[j] == +0 || quotient_coefficients[j] == -0 && j == 2)
- {
- rational_roots[element] = possible_roots[i];
- printf("\nRoot %lf at i = %d, j = %d\n", rational_roots[element], i, j);
- element++;
- }
- }
- printf("\n");
- }
- }