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.

Author nasix
Recipients nasix
Date 2011-05-10.18:16:39
SpamBayes Score 2.0666005e-06
Marked as misclassified No
Message-id <1305051403.14.0.965488921979.issue12052@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2011-05-10 18:16:43nasixsetrecipients: + nasix
2011-05-10 18:16:43nasixsetmessageid: <1305051403.14.0.965488921979.issue12052@psf.upfronthosting.co.za>
2011-05-10 18:16:39nasixlinkissue12052 messages
2011-05-10 18:16:39nasixcreate