Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,20 @@ def test_from_hex(self):
self.identical(fromHex('0X1.0000000000001fp0'), 1.0+2*EPS)
self.identical(fromHex('0x1.00000000000020p0'), 1.0+2*EPS)

# 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)
Copy link
Member

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.

Copy link
Member Author

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.

self.identical(fromHex('0x8p-1078'), 0.0)
self.identical(fromHex('0x8.0p-1078'), 0.0)
self.identical(fromHex('0x8.1p-1078'), TINY)
self.identical(fromHex('0x80p-1082'), 0.0)
self.identical(fromHex('0x81p-1082'), TINY)
self.identical(fromHex('.8p-1074'), 0.0)
self.identical(fromHex('8p-1078'), 0.0)
self.identical(fromHex('-.8p-1074'), -0.0)
self.identical(fromHex('+8p-1078'), 0.0)

def test_roundtrip(self):
def roundtrip(x):
return fromHex(toHex(x))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a corner case bug where the result of ``float.fromhex('0x.8p-1074')``
was rounded the wrong way.
4 changes: 2 additions & 2 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1463,8 +1463,8 @@ float_fromhex(PyTypeObject *type, PyObject *string)
bits lsb, lsb-2, lsb-3, lsb-4, ... is 1. */
if ((digit & half_eps) != 0) {
round_up = 0;
if ((digit & (3*half_eps-1)) != 0 ||
(half_eps == 8 && (HEX_DIGIT(key_digit+1) & 1) != 0))
if ((digit & (3*half_eps-1)) != 0 || (half_eps == 8 &&
key_digit+1 < ndigits && (HEX_DIGIT(key_digit+1) & 1) != 0))
round_up = 1;
else
for (i = key_digit-1; i >= 0; i--)
Expand Down