Why float yields 3.27826e-008 in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why float yields 3.27826e-008 in C++?

I have this little code: https://code.sololearn.com/c8YYukIFcRf4/?ref=app The purpose of function1() is to subtract 0.2 to the variable number, the problem is, when number hit 0.0, it doesn't print 0.0, prints 3.27826e-008, why? This is the output: 0.8 0.6 0.4 0.2 3.27826e-008 -0.2 And the conditional doesn't work, it seems that it never reaches 0, why it jumps from 0.2 to 3.27826e-008? Maybe is because I use a static variable? How can I make the conditional works ? I want to print 0.0 instead 3.27826e-008.

29th Jan 2019, 9:33 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
2 Answers
+ 1
Maybe it is the same problem: https://www.sololearn.com/Discuss/1642439/?ref=app "Decimal fractions can't be stored exactly on a binary computer. There is a precision error. For example, in some languages, if you tried this: x=1.0/3.0*3.0 you would get 0.999999999 instead of 1.0000000"
29th Jan 2019, 9:46 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Denise Roßberg thank you so much, it seems that this kind of problems it's because the computer can't handle in a proper way numbers with decimals, I fix it just setting a particular precision.
29th Jan 2019, 9:59 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar