-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
bpo-44954: Fix wrong result in float.fromhex corner case #27834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| # Regression test for a corner-case bug reported in b.p.o. 44954 | ||
| self.identical(fromHex('0x.8p-1074'), 0.0) | ||
| self.identical(fromHex('0x.80p-1074'), 0.0) | ||
| self.identical(fromHex('0x.81p-1074'), TINY) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to find what is the minimal example for non-zero result, but seems it works for arbitrary number of zeroes between 8 and 1: 0x.800000000000000000000...0000000000000000001p-1074.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that's reassuring, and as it should be: 0x.8p-1074 is the exact threshold for rounding, so if it's larger than 0x.8p-1074 by even the tiniest amount, it should round up.
|
Thanks @mdickinson for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10. |
|
GH-27854 is a backport of this pull request to the 3.10 branch. |
) (cherry picked from commit 60b93d9) Co-authored-by: Mark Dickinson <mdickinson@enthought.com>
|
GH-27855 is a backport of this pull request to the 3.9 branch. |
) (cherry picked from commit 60b93d9) Co-authored-by: Mark Dickinson <mdickinson@enthought.com>
This PR fixes a corner-case bug where the rounding logic in
float.fromhexresulted in a value that was rounded the wrong way. Assuming IEEE 754 binary64 floating-point, the bug can only occur in the case where:2**-107588is not preceded by any zerosExample bad inputs are
'0x.8p-1074','0x.80p-1074'and0x8p-1078. In these cases, the rounding logic tries to interpret thexcharacter as a hex digit, decides that as a hex digitxis odd, and so ends up rounding the value up to2**-1074, when it should have been rounded down to0.The case of a string that doesn't start with
0xis worse: for an input like.8p-1074or.80p-1074or8p-1078, the rounding logic examines the character before the beginning of the string to decide how to round.This bug affects all Python versions back to 2.7; the fix should be backported to 3.9 and 3.10, but I don't think it's critical enough to go into 3.10.0.rc2 - it can wait for 3.10.1.
https://bugs.python.org/issue44954