Python math is off by .000000000000045

Grant Edwards invalid at invalid.invalid
Wed Feb 22 21:48:34 CET 2012

On 2012-02-22, Alec Taylor <alec.taylor6 at> wrote:

> Simple mathematical problem, + and - only:
>>>> 1800.00-1041.00-555.74+530.74-794.95
> -60.950000000000045
> That's wrong.

Oh good.  We haven't have this thread for several days.

> Proof
> -60.95 aka (-(1219/20))
> Is there a reason Python math is only approximated?

Python uses binary floating point with a fixed size (64 bit IEEE-754
on all the platforms I've ever run across).  Floating point numbers
are only approximations of real numbers.  For every floating point
number there is a corresponding real number, but 0% of real numbers
can be represented exactly by floating point numbers.

> - Or is this a bug?

No, it's how floating point works.

If you want something else, then perhaps you should use rationals or

Grant Edwards               grant.b.edwards        Yow! What I want to find
                                  at               out is -- do parrots know
                                much about Astro-Turf?

More information about the Python-list mailing list