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() erroneous for some large arguments
Type: behavior Stage:
Components: None Versions: Python 2.5
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, nasix
Priority: normal Keywords:

Created on 2011-05-10 18:16 by nasix, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg135724 - (view) Author: Neil (nasix) Date: 2011-05-10 18:16
round() returns incorrect results for certain large real-integer arguments. Demonstration via interpreter session:
>>> x = 2.0**52+1  # Huge, odd double integer near limits of what IEEE format can fully represent in its mantissa part
>>> round(x) - x   # Difference should be zero
1.0
>>> x = 2.0**53+1  # Even larger odd argument ...
>>> round(x) - x   # ... returns correct result!
0.0
>>> x = 2.0**51+1  # And a smaller argument ...
>>> round(x) - x   # ... also returns correct result!
0.0

Discussion:
It is not sufficient to implement round(x) as ceil(x - .5) for negative x and floor(x + .5) otherwise, since large integers near the representation limits will be changed incorrectly by these operations, apparently due to internal FPU semantics. One must test the argument first to see if it is a floating-point integer (e.g., math.floor(x) == x), and only bias & truncate towards zero if it is not. C math libraries that implement round(), such as on Linux & MacOS, do this correctly.
msg135725 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-05-10 18:20
Please upgrade:  this issue is already fixed in current versions of Python.
msg135726 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-05-10 18:25
(Duplicate of issue 7070.)
History
Date User Action Args
2022-04-11 14:57:17adminsetgithub: 56261
2011-05-10 18:25:24mark.dickinsonsetmessages: + msg135726
2011-05-10 18:20:03mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg135725

resolution: out of date
2011-05-10 18:16:39nasixcreate