Python math is off by .000000000000045

Benjamin Kaplan benjamin.kaplan at case.edu
Wed Feb 22 19:32:06 CET 2012


On Feb 22, 2012 1:16 PM, "Alec Taylor" <alec.taylor6 at gmail.com> wrote:
>
> Simple mathematical problem, + and - only:
>
> >>> 1800.00-1041.00-555.74+530.74-794.95
> -60.950000000000045
>
> That's wrong.
>
> 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?
>
> Thanks for all info,
>
> Alec Taylor
> --

You aren't doing math with  decimal numbers. you're using IEEE
754-compliant double precision floating point numbers. this isn't just a
python thing. You'd get the same results in C, Java, VB, and pretty much
every other general purpose language written in the last 40 years.

Floats are represented in a form similar to scientific notation (a * 2^b),
so just like scientific notation, there's a finite number of significant
figures. And just like there are rational numbers that can't be represented
in decimal, like 1/3, there are numbers that can't be represented in
binary, like 1/10.

Double-precision numbers are accurate to about 15 decimal digits. if you
need more precision, there is the decimal module but it's way slower
because your processor doesn't natively support it.

> http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120222/06afb03f/attachment.html>


More information about the Python-list mailing list