Inconsistent results from int(floatNumber)

Ian ian.g.kelly at gmail.com
Mon Oct 25 23:58:47 CEST 2010


On Oct 25, 3:44 pm, gershar <gerrys... at gmail.com> wrote:
> It looks like a rounding problem but on the surface there is nothing
> to round. I am aware that there are rounding limitations with floating
> point arithmetic but the value passed to int() is always correct.

No, it isn't:

>>> for x in xrange(5):
...   i += 0.1
...   z = i * 10.0
...   print
...   print z
...   print repr(z)
...   print int(z)
...

-499.0
-499.0
-499

-498.0
-498.0
-498

-497.0
-496.99999999999994
-496

-496.0
-495.99999999999994
-495

-495.0
-494.99999999999994
-494

Cheers,
Ian



More information about the Python-list mailing list