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[color=blue][color=green][color=darkred]
>>> round(0.0225, 3)[/color][/color][/color]
0.023[color=blue][color=green][color=darkred]
>>> "%.3f" % round(0.0225, 3)[/color][/color][/color]
'0.023'[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
$ python2.4
Python 2.4.1 (#2, Jul 12 2005, 09:22:25)
[GCC 4.0.1 (Debian 4.0.1-1)] on linux2[color=blue][color=green][color=darkred]
>>> round(0.0225, 3)[/color][/color][/color]
0.0219999999999 99999[color=blue][color=green][color=darkred]
>>> "%.3f" % round(0.0225, 3)[/color][/color][/color]
'0.022'[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
(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
$ 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[color=blue][color=green][color=darkred]
>>> round(0.0225, 3)[/color][/color][/color]
0.023[color=blue][color=green][color=darkred]
>>> "%.3f" % round(0.0225, 3)[/color][/color][/color]
'0.023'[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
$ python2.4
Python 2.4.1 (#2, Jul 12 2005, 09:22:25)
[GCC 4.0.1 (Debian 4.0.1-1)] on linux2[color=blue][color=green][color=darkred]
>>> round(0.0225, 3)[/color][/color][/color]
0.0219999999999 99999[color=blue][color=green][color=darkred]
>>> "%.3f" % round(0.0225, 3)[/color][/color][/color]
'0.022'[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
(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
Comment