floating point arithmetic

Rob Clewley rob.clewley at gmail.com
Tue Aug 26 17:24:17 EDT 2008


> I understand that due to different arithmetic used in floating points
> they are just approximations. Hence, 180/100=1 in my python interpreter.

No, that's not the reason you get 1, it's because the current version
of python does integer division by default. Try doing 180.0/100 or
including

from __future__ import division

at the top of your scripts before dividing your numbers.

> How can I tackle this problem of inaccurate floating point numbers?

There are few occasions for "regular" users to worry about the
inaccuracy of floating point, unless you are doing very technical
calculations at high precision. If you need decimals to be represented
"perfectly" in python (e.g. you are writing scripts for financial
applications), try importing the decimal package (look it up in the
python docs).

-Rob



More information about the Python-list mailing list