473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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

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
2 1246
weaknessforcats
9,208 Expert Mod 8TB
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
donbock
2,426 Expert 2GB
+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

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

Similar topics

6
by: Grant | last post by:
I am connecting to an access database using a datareader in C#. I get results when I run a certain query from Access but when I run it from Code it does not retrieve any results. I have put a stop...
0
by: Joby | last post by:
Dear All, My problem is first i download an application, that application executes in the client machine and it returns some values. I want to send it back to server and validate these values in...
21
by: Jim | last post by:
I am trying to write an HTTP/HTTPS proxy server in VB.Net 2005. But, I don't really even know how the internal workings of a proxy should act. Does anyone have anything on the protocols used in...
0
by: Hulk | last post by:
I have problems passing parameters from C# application to XSL file and doing a transformation. My code is below.: XsltArgumentList xslArg = new XsltArgumentList(); String key =...
1
by: sathyan8294 | last post by:
what is vb.net code for changing the values in datagrid and save the changed values using vb.net windows application
1
by: Mel | last post by:
On the SERVER SIDE, I need to get some values from form Elements and save the values into a file. Is this possible using Javascipt ? any exmple is highly appreciated
4
by: djpaul | last post by:
Hello, I'm creating a program for school. But it's works with a tekst file. In a certain section i need some values of a tekst line to draw lines. Te line is someting like: 345543; Old wood; ...
8
by: ahilar12 | last post by:
Hi experts, I have a form with many textboxes,listboxes in php.I have a edit button to edit the values in the form.once i click the edit button the existing values should be displayed so that...
5
by: darkestknight43 | last post by:
Hello, I've been working on this piece of code for quite a while. Essentially it is a Search Tool which pulls Mobile Home information from a database and displays listings based on the users input....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.