Python math is off by .000000000000045

Christian Heimes lists at cheimes.de
Wed Feb 22 19:26:26 CET 2012


Am 22.02.2012 19:13, schrieb Alec Taylor:
> Simple mathematical problem, + and - only:
> 
>>>> 1800.00-1041.00-555.74+530.74-794.95
> -60.950000000000045
> 
> That's wrong.

That's only the correct answer for unlimited precision, not for IEEE-754
semantics. http://en.wikipedia.org/wiki/IEEE_754

> Proof
> http://www.wolframalpha.com/input/?i=1800.00-1041.00-555.74%2B530.74-794.95
> -60.95 aka (-(1219/20))
> 
> Is there a reason Python math is only approximated? - Or is this a bug?

Python uses the platforms double precision float datatype. Floats are
almost never exact.

Christian




More information about the Python-list mailing list