round() wrong in Python 2.4?

Nils Grimsmo nils.grimsmo at gmail.com
Tue Sep 13 09:17:50 EDT 2005


Why did round() change in Python 2.4?

$ python2.3
Python 2.3.5 (#2, Jun 19 2005, 13:28:00)
[GCC 3.3.6 (Debian 1:3.3.6-6)] on linux2
>>> round(0.0225, 3)
0.023
>>> "%.3f" % round(0.0225, 3)
'0.023'
>>>
$ python2.4
Python 2.4.1 (#2, Jul 12 2005, 09:22:25)
[GCC 4.0.1 (Debian 4.0.1-1)] on linux2
>>> round(0.0225, 3)
0.021999999999999999
>>> "%.3f" % round(0.0225, 3)
'0.022'
>>>

(Is this due to the different GCC used?)

How do you correctly output floating-point numbers in 2.4?

I do not like the "print number + EPS" solution, as you would need
different EPS for different exponent sizes. In C you could get it by
taking integer 1, and &-ing in the right exponent, and then casting to
double via void*. This would not be very portable, though.


Klem fra Nils




More information about the Python-list mailing list