Why is 2.7182818284590452354 being printed as 2.7182818284590450908? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Why is 2.7182818284590452354 being printed as 2.7182818284590450908?

I created a constant Eu using this : #define Eu 2.7182818284590452354 //const long double Eu = 2.7182818284590452354; But on printing it like this, I get: long double a=Eu; cout.setf(ios::fixed); cout.precision(19); cout<<a<<endl; // a is printed as 2.7182818284590450908. Why? Why is there such precision loss in just printing a constant? Also, though its log to base e should be 1 (No decimal part), this becomes false: if(log(a)==static_cast<int>(log(a))) cout<<"Yes"<<endl; Why is this happening?

9th May 2017, 11:59 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
11 Answers
+ 2
Comparing floating point using == operator would be always getting false return due to accuracy issue. The better way to do is give the return certain tolerance value. if you compare 2 double d1 and d2. you can set some tolerance value t if(abs (d1-d2) < t ) return true; t value would be 0.00001 (depends how precise you want, but cannot be too precise due to the computation error)
9th May 2017, 1:36 PM
Calviղ
Calviղ - avatar
+ 11
By design, C++ uses IEEE floating point types which are only precise to a certain degree. This is something that you may want to try out though: http://stackoverflow.com/questions/4738768/printing-double-without-losing-precision
9th May 2017, 12:11 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
@Calvin Then can I ever find out, if log to the base e of a number is an integer or not, using a program?
9th May 2017, 12:43 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
@Calvin Then, what can I use for Euler's number? As this code is just the bug part, and the real variable a is an element of a 2*2 long double Matrix and I wanted to test if the elements can satisfy a condition... Guess I'll have to limit such conditions...
9th May 2017, 12:51 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
The program works.! I tried rounding off a to the nearest 1/100000000 and the equation became satisfied... a=floor(a*100000000)/100000000.0; @Calvin & @Hatsy Rei Thanks a lot for your help!
9th May 2017, 1:11 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
@Calvin I'll try this too... Thank You!
9th May 2017, 1:37 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
@Hatsy Rei That didn't work for me... :(
9th May 2017, 12:22 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
I read now that the maximum precision for long double is 17, and so I reduced it to 16 to be on a safe side... But now, though log(a) is printed as 1, which it should be, log(a)==static_cast<int>(log(a)) remains false... Why? cout.precision(16); cout<<a; //This prints 2.718281828459045 cout<<log(a); //This prints 0.999999999999999 //Perhaps the passed value does not match with the value stored in C++ for Euler's number, Which I had copied earlier for use... How does C++ use M_E with precision then? Why ain't I able to do so?
9th May 2017, 12:36 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
Cpu computing is never too accurate. Like @Hatsy said, the precision can only go down to certain degree. You can take a look of this program testing.. https://code.sololearn.com/Wg47NBJNari7/?ref=app It shows that the precision point can only go to maximum of 15 decimal point, More then that the accuracy is not guaranteed.
9th May 2017, 12:39 PM
Calviղ
Calviղ - avatar
+ 4
@Kinshuk Imho, I think we should not use floating point for more then 15 decimal point in our program.
9th May 2017, 12:48 PM
Calviղ
Calviղ - avatar
+ 4
Just reduce your constant number to 15 decimal points. The result is only accurate for 15 dp or less.
9th May 2017, 1:07 PM
Calviղ
Calviղ - avatar