Inverse confusion about floating point precision

Skip Montanaro skip at pobox.com
Mon May 9 13:38:04 EDT 2005


I understand why the repr() of float("95.895") is "95.894999999999996".
What I don't understand is why if I multiply the best approximation to
95.895 that the machine has by 10000 I magically seem to get the lost
precision back.  To wit:

    % python
    Python 2.3.4 (#12, Jul  2 2004, 09:48:10) 
    [GCC 3.3.2] on sunos5
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 95.895
    95.894999999999996
    >>> 95.895 * 10000
    958950.0

Why isn't the last result "958949.99999999996"?  IOW, how'd I get back the
lost bits?

Thx,

Skip



More information about the Python-list mailing list