subtraction of floating point numbers

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Fri Feb 24 03:49:15 EST 2012


Jaroslav Dobrek <jaroslav.dobrek at gmail.com> writes:

> when I have Python subtract floating point numbers it yields weird
> results. Example:
>
> 4822.40 - 4785.52 = 36.8799999999992

We've had this discussion here one or two days ago...

The usual answer is: please read "What Every Computer Scientist Should
Know About Floating Point Arithmetic", at:

http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.22.6768

and check the answers posted these last days. In brief: you're working
with floating point numbers, not reals (i.e., real "reals"). That's
life. Deal with it, or move to specialized packages, like decimal.

-- Alain.



More information about the Python-list mailing list