floating point in 2.0

Michael P. Soulier msoulier at storm.ca
Tue Jun 5 05:28:54 CEST 2001


    Greetings people. 

    My apologies if this has been covered. 

    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. 
    Could someone explain this behaviour?

    Much appreciated and cheers,

    Mike

-- 
Michael P. Soulier <msoulier at storm.ca> 
"With sufficient thrust, pigs fly just fine. However, this is not necessarily a
good idea. It is hard to be sure where they are going to land, and it could be
dangerous sitting under them as they fly overhead." -- RFC 1925



More information about the Python-list mailing list