I try this code with python 2.7.9:
a=8.52
for i in range(1,3):
a *= 10.0
print int (a)
It should display
85
852
But it displays
85
851
Do you have any idea why? Is it a known bug?
I try this code with python 2.7.9:
a=8.52
for i in range(1,3):
a *= 10.0
print int (a)
It should display
85
852
But it displays
85
851
Do you have any idea why? Is it a known bug?
Do you have any idea why? Is it a known bug?
The obvious print
debugging approach yields:
a=8.52
for i in range(1,3):
a *= 10.0
print int (a)
print a
which prints:
85
851
851.99999...
So, what's happening here is floating point math. It's not exact, because there's no exact way to represent 8.51
with binary floating point. Instead, a value very very close (yet smaller) to 8.51
is first stored in a
.