Thanks for the report.
This is fixed in Python 3.1 (except on a few unusual hardware/OS
combinations):
Python 3.1rc1+ (py3k:73252, Jun 6 2009, 10:35:36)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> round(697.04157958254996, 10)
697.0415795825
It's a "won't fix" for Python 2.x. Rounding to 10 decimal places
involves multiplying by 10**10., rounding to the nearest integer, then
dividing by 10**10. again; the problem in this case is that
multiplication by 10.**10 is inexact, and the error involved in the
multiplication by 10**10. pushes the value from the 'round down' region
into the 'round up region':
Python 2.7a0 (trunk:73252, Jun 6 2009, 10:16:08)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 697.04157958254996 * 10**10
6970415795825.5
[38623 refs]
There's no easy way to fix this, short of using multiple-precision
arithmetic to do the rounding (which is what Python 3.1 does).
By the way, Python's float can't represent the number
697.04157958254996
exactly (assuming IEEE 754 arithmetic): the closest it gets is the
value:
697.041579582549957194714806973934173583984375
but the repr of a float (in Python 3.0 and 2.x) only produces 17
significant digits, since that's enough to guarantee that the
repr() value evaluates back to the correct float (assuming correct
rounding).
|