round in 2.6 and 2.7

Hrvoje Niksic hniksic at xemacs.org
Fri Dec 24 04:08:36 EST 2010


"Martin v. Loewis" <martin at v.loewis.de> writes:

>> 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.
>
> The 2.6 result is simply incorrect.
[...]
> Yes. It's a bug fix described in "What's new in Python 2.7" thus:

That makes sense, thanks.  Even within Python 2.6, rounding with
different methods could produce inconsistent results:

>>> round(9.95, 1)
10.0
>>> '%.1f' % 9.95
'9.9'



More information about the Python-list mailing list