Message 158717 - Python tracker (original) (raw)
Well this IS a bug.
I assume that you're referring to behaviour like this:
Python 2.7.2 (default, Jan 13 2012, 17:11:09) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information.
x = 2.545 round(x, 2) 2.54
To explain again, what happens here is:
(1) After the assignment 'x = 2.545', what's stored for x is not the precise decimal value 2.545, but a binary approximation to it. That binary approximation just happens to be very slightly less than 2.545.
(2) Now when rounding, the usual rules are applies (values less than half get rounded down), to give 2.54.
Which part(s) of the above do you think should be changed? Should the 'round' function incorrectly round some numbers up even though they fall below the halfway case?