floating point in 2.0

Remco Gerlich scarblac at pino.selwerd.nl
Tue Jun 5 06:12:50 EDT 2001


Michael P. Soulier <msoulier at storm.ca> wrote in comp.lang.python:
>     Greetings people. 
> 
>     My apologies if this has been covered. 

And covered again, and again, and again, ...

>     In Python 1.5.2, I could do this:
> 
> good = (0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)
> 
>     I could then get back exactly this when I output the value of "good".
> However, with Python 2.0, I get this:
> 
> >>> good
> (0.0, 0.10000000000000001, 0.20000000000000001, 0.29999999999999999,
> 0.40000000000000002, 0.5, 0.59999999999999998, 0.69999999999999996,
> 0.80000000000000004, 0.90000000000000002, 1.0)
> 
>     Now, I'm assuming that this has something to do with the way that Python
> 2.0 stores floating-point numbers, but this could get confusing during
> debugging.

The way they're stored hasn't changed, it's just that 2.0 doesn't lie
anymore when writing the repr() of a float. This is how the floats are
represented internally, there is no exact binary representation for any of
them except 0.5.

There is something on it in the FAQ (see inaccurate floating point or
something like that), or use Google to find a post about it - about half the
posts in recent months were on this subject, it seems :-)

-- 
Remco Gerlich



More information about the Python-list mailing list