round in 2.6 and 2.7
Martin v. Loewis
martin at v.loewis.de
Thu Dec 23 14:50:50 EST 2010
> 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. 9.95 cannot be represented as a
floating point number; the number that is closest to it is actually
smaller than 9.95. Rounding that number should give 9.9 (or something
close to it - 9.9 cannot be represented, either), not 10.0.
>>>> round(9.95, 1)
> 9.9
>
> Is the change to round() expected?
Yes. It's a bug fix described in "What's new in Python 2.7" thus:
Float-to-string and string-to-float conversions are correctly rounded.
The round() function is also now correctly rounded.
Not sure that this is correct English; I think it means that the
round() function is now correct.
Regards,
Martin
More information about the Python-list
mailing list