[Tutor] floating point rounding inconsistency

Oscar Benjamin oscar.j.benjamin at gmail.com
Fri Sep 28 13:56:32 CEST 2012


On 19 September 2012 19:27, Andrew Tritt <ajtritt at lbl.gov> wrote:

> 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?
>

It is because Python (like all programming languages I know) represents
floating point numbers in base 2. This is discussed in the python.orgtutorial:
http://docs.python.org/tutorial/floatingpoint.html

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120928/51951690/attachment.html>


More information about the Tutor mailing list