# Calculating polynomial roots

• 08-23-2004
gambit
Calculating polynomial roots
Sorry if this seems a bit basic, I'm pretty new to all this etc. I'm trying to write a programme which can read in real coefficients of a 2nd order polynomial (ax^2 + bx + c = 0) and produce its roots in terms of x1 and x2.. this is what I have so far:

Code:

```#include<iostream.h> #include<conio.h> #include<stdio.h> #include<math.h> main () { int ia,ib,ic; float a, b, c; float x1, x2; float s1; cout<<"Polynomial task"<<endl; cout<<" "<<endl; cout<<"x1 = -b/2a + sqrt((b^2/4a^2) - c/a)"<<endl; cout<<"x2 = -b/2a - sqrt((b^2/4a^2) - c/a)"<<endl; cout<<" "<<endl; cout<<"Choose a value for a:"<<endl; cin>>ia; if (ia==0) {         cout<<"Division by 0 is not possible, please choose another value."<<endl;         getch();         return(0); } cout<<"Choose a value for b:"<<endl; cin>>ib; cout<<"Choose a value for c:"<<endl; cin>>ic; a = (float)ia; b = (float)ib; c = (float)ic; s1 = sqrt( ( (ib^2) / (4 * (ia^2)) ) - (c/a) ); if (s1 < 0) {         cout<<"No possible solution for these values"<<endl;         getch();         return(0); } x1 = -(b / (2*a) ) + s1; cout<<" "<<endl; cout<<"x1 ="<<endl; cout<<x1<<endl; x2 = -(b / (2*a) ) - s1; cout<<" "<<endl; cout<<"x2 ="<<endl; cout<<x2<<endl; getch(); return(0); }```
The values for a, b and c I have are:

5, 3, -1
-3, 27, 0.5
5, 0, 0
5, 50, 0
1, 4, 4
0, 4, 4
0, 0, 0
0, 2.74, 0

However the values I'm getting when I plug in these coefficients for x1 and x2 are way off the mark. I'm not really sure where I'm going wrong. Any help is appreciated. :(
• 08-23-2004
Salem
> ia^2
^ is not "raise to power" in C
Use the math function
pow( ia, 2 )
• 08-23-2004
gambit
Okay thanks a bunch, that's pretty much cleared it up.. though there's one value which isn't quite right. All are spot on except for the ones for

-3, 27, 0.5

I have the values as x1 = 9.01848 and x2 = -0.01848, the 2 on my screen are 9 and 0, which are obviously close but is there any way to get those exact values up?
• 08-23-2004
Micko
I've change your code a little bit so I can compile it on Visual Studio .Net. I hope you don't mind.
First I suggest you reconsider the approach to this problem.
But because you're a begineer that's OK.
There are some mistakes in your code. First math operation power like 4^2=16 is actually function pow(4,2) in standard C math. Operator "^" is a bitwise binary operator and represents logical EXCLUSIVE OR operation which means:
4^2 is equal in binary:
4 ->0100
2->0010
and 4^2= 0100
^0010
----
0110
0110 binary represents number 6 in decimal number system.
EXCLUSIVE OR operation results in 0 if both operand are the same (both 0 or both 1) otherwise it results is 1.

So simple use function pow().
And I changed the way how s1 is calculated. I'm sure you'll understand.
And always use int main() according to C standard not just main().
I saw some other things but that is not crucial at the moment.
Cheers
• 08-23-2004
Davros
>I have the values as x1 = 9.01848 and x2 = -0.01848, the 2 on my screen are 9 and 0

The sizes of error in the return values indicate there is an error in your code, rather than just being a question of accuracy.

You should also be aware that solutions to a quadratic can be complex. I.e., if the value you pass into the sqrt function is negative, the solutions are going to be complex. The standard sqrt call cannot calculate imaginary roots and will set an error in the errno global variable if you pass it a negative value, but otherwise it will return an incorrect value or NAN if your compliler supports this.
• 08-23-2004
Micko
I totaly agree with you Davros. That's why I wrote: "I saw some other things but that is not crucial at the moment.". Maybe he/she didn't want to consider complex roots. Maybe just wants solutionsto be from set of real numbers!
• 08-23-2004
XSquared
The reason that you're not getting the right answers is because you're getting the values from the user as an int, when you should be reading a float. Thus, when you enter 0.5, it truncates it to 0.
• 08-23-2004
gambit
Thanks much guys/gals, got it working fine now.