round() wrong in Python 2.4?

Robert Kern rkern at ucsd.edu
Tue Sep 13 10:50:17 EDT 2005


Jeremy Sanders wrote:
> Nils Grimsmo wrote:
> 
>>Why did round() change in Python 2.4?
> 
> It the usual floating point representation problem. 0.0225 cannot be
> represented exactly:

That's not what he's asking about. He's asking why his Python 2.3 rounds
0.0225 *up* to 0.023 while his Python 2.4 rounds *down* to 0.022. It's
the change in behavior that he's concerned with and isn't just the usual
floating point problem.

I'm going to suggest that it's a platform issue, possibly the change in
compiler. I get identical results on OS X with both versions of Python
both compiled by gcc-3.3 .

[~]$ python2.3
Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.0225
0.022499999999999999
>>> round(0.0225, 3)
0.023
>>>
[~]$ python2.4
Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.0225
0.022499999999999999
>>> round(0.0225, 3)
0.023
>>>

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list