Thread: how can float limit be not enough?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    18

    how can float limit be not enough?

    Here is my first own programme,i was trying to calculate error of finding roots for a second degree equation.I used float type for variables to see all result but i cant see results after some numbers.anyone can help this poor beginner with this?

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    int main()
    { 
        int i;
        float p=1,X11,X12,E,Er;
        
        for (i=0; i<10; i++)
        { 
          p=p*10;  
          X11= -(p/2)- sqrt ((pow(p,2)/4)-1);
          X12= 1 /((-(p/2)+sqrt ((pow(p,2)/4)-1)));
          E= X11-X12;
          Er= E/X11;
          
    
          cout << "X^2-"<<p<<"x+1=0"<<endl;
          cout <<"X11="<<X11<<'\t';
          cout <<"X12="<<X12<<endl;
          cout <<"E="<<E<<'\t'<<"Er="<<Er<<endl;
          
        }     
        std::cout <<"Press [Enter] to continue" <<std::endl;
        std::cin.get();
        
    
        return 0;
    }

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    The sqrt function can not handle imaginary numbers. That is, it does not work properly with any negative numbers once everything in the sqrt has been evaluated.

    You will have to break those down (if evaluated stuff under square root is less than 0)
    You can either then do a little different math and output the sqrt based on “i”, or just say it is imaginary.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    but in this programme p always has a pozitive value and sqrt is always pozitive too..isnt it?

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Hmmm, you are right.

    A little examination for type double the maximum you can have in the sqrt function is 20 digits. You hit that limit quickly because you are squaring ‘p’.

    I do not know why that is the sqrt function limit; but I am sure sometime someone will come along and tell us why!

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    18

    Unhappy

    it seems noone wants to help this out...

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use a double.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The problem is that pow(p,2) can overflow. Computing the sqrt() of the result of an overflow does not magically recover things.

    Replace these lines;
    Code:
          X11= -(p/2)- sqrt ((pow(p,2)/4)-1);
          X12= 1 /((-(p/2)+sqrt ((pow(p,2)/4)-1)));
    with (if I've done the algebra correctly, and then mapped it back to code correctly);
    Code:
        double temp = sqrt(p+2)*sqrt(p-2);   // assumes p >= 2
        X11= -(p + temp)/2.0;
        X12=  2.0/(temp - p);
    This might still fail for large values of p though: adding a large value to a small one can still yield overflow.

    PS Be patient in waiting for a response. Few of us are sitting here continually: you get an answer when we read your post. If you want instantaneous response, hire someone to be at your beck and call.
    Last edited by grumpy; 10-08-2005 at 11:59 PM.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    Thanks a lot, i got you..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM