[Tutor] floating point rounding inconsistency

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Sep 28 14:15:56 CEST 2012


On 28/09/2012 12:56, Oscar Benjamin wrote:
> 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
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

The Python round function is itself problematic.  The idea of 
deprecating it is currently being discussed on Python ideas.  This quote 
from Calvin Spealman is typical "Also, I'd be completely in support of 
dropping round() and agree it gets misused
and leads to too much confusion. We should promote the right ways, and 
sometimes to show the right path you need to lock another door and throw
away the key.".

-- 
Cheers.

Mark Lawrence.



More information about the Tutor mailing list