[Tutor] floating point rounding inconsistency

Andrew Tritt ajtritt at lbl.gov
Wed Sep 19 20:27:32 CEST 2012


Hello,

I am new to python, and I was experimenting with the round function, and
came across what appears to be a bug in floating point rounding. I am
guessing there is a valid explanation for it.

When I round the floating points 10.6[0-9]5 to two decimal places, it
rounds as expected for 6 of the 10, but not for the other 4. When I try the
same for 10.7[0-9]5, I experience the same problem, but for 5 of the 10
possibilties, and not for the analogous floats.

Also, python storing the numbers as they are represented at the prompt.
i.e. 10.665 is stored as 10.665, not something like 10.665000001 or
10.66499999999.

Can anyone explain to me what's happening?

$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on
darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> round(10.605,2)
10.61
>>> round(10.615,2)
10.62
>>> round(10.625,2)
10.63
>>> round(10.635,2)
10.63
>>> round(10.645,2)
10.64
>>> round(10.655,2)
10.65
>>> round(10.665,2)
10.66
>>> round(10.675,2)
10.68
>>> round(10.685,2)
10.69
>>> round(10.695,2)
10.7
>>> round(10.705,2)
10.71
>>> round(10.715,2)
10.71
>>> round(10.725,2)
10.72
>>> round(10.735,2)
10.73
>>> round(10.745,2)
10.74
>>> round(10.755,2)
10.76
>>> round(10.765,2)
10.77
>>> round(10.775,2)
10.78
>>> round(10.785,2)
10.79
>>> round(10.795,2)
10.79
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120919/8859559b/attachment.html>


More information about the Tutor mailing list