[issue5118] '%.2f' % 2.545 doesn't round correctly

Mark Dickinson report at bugs.python.org
Thu Apr 19 13:54:12 CEST 2012


Mark Dickinson <dickinsm at gmail.com> added the comment:

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

----------
assignee:  -> mark.dickinson
versions: +Python 2.7 -Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5118>
_______________________________________


More information about the Python-bugs-list mailing list