Thread: Calculating polynomial roots

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    3

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > ia^2
    ^ is not "raise to power" in C
    Use the math function
    pow( ia, 2 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    3
    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?

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    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.
    Read the FAQ for more infos.

    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

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >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.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    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!

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    3
    Thanks much guys/gals, got it working fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie - cubic polynomial program - help!
    By jaffa in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 05:52 AM
  2. Entering in an nth degree polynomial
    By Noah in forum C Programming
    Replies: 1
    Last Post: 03-02-2006, 09:02 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Polynomial
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 05:00 PM
  5. Polynomial Problem
    By softcoder76 in forum C++ Programming
    Replies: 5
    Last Post: 03-01-2002, 02:07 PM