12.02.2017, 18:35
Hi, when I set a float to 0.005 and print it, it says "0.004999". Is there any way to fix that? I need it to be exactly 0.005.
It's the same thing as trying to display 1/3 in base 10: 0.33333... You know that 1/3 + 1/3 + 1/3 equals 1 but a computer will say its 0.33 + 0.33 + 0.33 = 0.99999... That's just how floating point arithmetic works. As far as I know it's not possible to round the fractional part.
http://www.youtube.com/watch?v=PZRI1IfStY0 |