# how can float limit be not enough?

• 10-08-2005
turkertopal
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; }```
• 10-08-2005
Enahs
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.
• 10-08-2005
turkertopal
but in this programme p always has a pozitive value and sqrt is always pozitive too..isnt it?
• 10-08-2005
Enahs
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!
• 10-08-2005
turkertopal
it seems noone wants to help this out...
• 10-08-2005
dwks
Use a double.
• 10-08-2005
grumpy
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.
• 10-09-2005
turkertopal
Thanks a lot, i got you..