This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: round() error
Type: behavior Stage:
Components: Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, steve21
Priority: normal Keywords:

Created on 2009-06-07 07:44 by steve21, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg89029 - (view) Author: Steve (steve21) Date: 2009-06-07 07:44
I wish to round the float 697.04157958254996 to 10 decimal digits after
the decimal point.

$ python3.0
Python 3.0.1 (r301:69556, Jun  7 2009, 14:51:41)
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> 697.04157958254996
697.04157958254996      # python float can represent this number exactly

>>> 697.04157958250000  # this is the expected result
697.04157958250005      # this is the closest python float representation

>>> round(697.04157958254996, 10)
697.04157958259998      # error

round() gives a result that is closer to
697.0415795826
than the expected result of
697.0415795825
- it has not rounded to the closest 10th decimal digit after the decimal
point.

(python 2.6.2 has the same problem)
msg89034 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-06-07 14:00
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).
msg89040 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-06-07 14:37
See also issue 1869 and the issues it links to for additional discussions 
about round.
History
Date User Action Args
2022-04-11 14:56:49adminsetgithub: 50477
2009-06-07 14:37:23mark.dickinsonsetmessages: + msg89040
2009-06-07 14:00:32mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg89034

resolution: wont fix
2009-06-07 07:44:03steve21create