round in 2.6 and 2.7

Hrvoje Niksic hniksic at xemacs.org
Thu Dec 23 13:57:13 EST 2010


I stumbled upon this.  Python 2.6:

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 9.95
9.9499999999999993
>>> "%.16g" % 9.95
'9.949999999999999'
>>> round(9.95, 1)
10.0

So it seems that Python is going out of its way to intuitively round
9.95, while the repr retains the unnecessary digits.  However, with 2.7
it's exactly the opposite:

Python 2.7.0+ (r27:82500, Sep 15 2010, 18:04:55)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 9.95
9.95
>>> "%.16g" % 9.95
'9.949999999999999'
>>> round(9.95, 1)
9.9

Is the change to round() expected?



More information about the Python-list mailing list