[Python-Dev] rounding problem

Gary Herron gherron at islandtraining.com
Mon May 31 21:39:20 EDT 2004


On Monday 31 May 2004 06:22 pm, Norlin Abd Rahim wrote:
> the number actually comes from multiplication between 237.50 and 4.762
> which will get 1130.975. So it suppose to be 1130.98 isnt it if it'll get
> rounded to 2 decimal places.

But...

Due to the wonders and inaccuracies of binary floating point
representations on digital computers, 4.762 can not be represented
EXACTLY.  The closest it can get is 4.7619999999999996, and that's
where you're inaccuracy comes from.  In the following interactive
python session, the values printed out are printed out to their full
precision (that's a often misunderstood feature of the python
interactive mode).  You can see where the floating point inaccuracies
are introduced.

>>> 237.50
237.5
>>> 4.762
4.7619999999999996
>>> 237.50 * 4.762
1130.9749999999999
>>>

Just for clarification .. This is NOT a problem with Python, but is a
feature of the binary representation of floating point numbers and can
be found on all computers and in all programming languages.

Gary Herron





More information about the Python-Dev mailing list