Skip to content

Commit fc33d4c

Browse files
committed
Issue python#15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
1 parent cb0ec7d commit fc33d4c

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

Lib/decimal.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,13 @@ def __rfloordiv__(self, other, context=None):
16011601

16021602
def __float__(self):
16031603
"""Float representation."""
1604-
return float(str(self))
1604+
if self._isnan():
1605+
if self.is_snan():
1606+
raise ValueError("Cannot convert signaling NaN to float")
1607+
s = "-nan" if self._sign else "nan"
1608+
else:
1609+
s = str(self)
1610+
return float(s)
16051611

16061612
def __int__(self):
16071613
"""Converts self to an int, truncating if necessary."""

Lib/test/test_decimal.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,22 @@ def test_tonum_methods(self):
19421942
for d, n, r in test_triples:
19431943
self.assertEqual(str(round(Decimal(d), n)), r)
19441944

1945+
def test_nan_to_float(self):
1946+
# Test conversions of decimal NANs to float.
1947+
# See http://bugs.python.org/issue15544
1948+
Decimal = self.decimal.Decimal
1949+
for s in ('nan', 'nan1234', '-nan', '-nan2468'):
1950+
f = float(Decimal(s))
1951+
self.assertTrue(math.isnan(f))
1952+
sign = math.copysign(1.0, f)
1953+
self.assertEqual(sign, -1.0 if s.startswith('-') else 1.0)
1954+
1955+
def test_snan_to_float(self):
1956+
Decimal = self.decimal.Decimal
1957+
for s in ('snan', '-snan', 'snan1357', '-snan1234'):
1958+
d = Decimal(s)
1959+
self.assertRaises(ValueError, float, d)
1960+
19451961
def test_eval_round_trip(self):
19461962
Decimal = self.decimal.Decimal
19471963

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Core and Builtins
3232
Library
3333
-------
3434

35+
- Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
36+
3537
- Issue #15776: Allow pyvenv to work in existing directory with --clean.
3638

3739
- Issue #15249: BytesGenerator now correctly mangles From lines (when

Modules/_decimal/_decimal.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3357,7 +3357,23 @@ PyDec_AsFloat(PyObject *dec)
33573357
{
33583358
PyObject *f, *s;
33593359

3360-
s = dec_str(dec);
3360+
if (mpd_isnan(MPD(dec))) {
3361+
if (mpd_issnan(MPD(dec))) {
3362+
PyErr_SetString(PyExc_ValueError,
3363+
"cannot convert signaling NaN to float");
3364+
return NULL;
3365+
}
3366+
if (mpd_isnegative(MPD(dec))) {
3367+
s = PyUnicode_FromString("-nan");
3368+
}
3369+
else {
3370+
s = PyUnicode_FromString("nan");
3371+
}
3372+
}
3373+
else {
3374+
s = dec_str(dec);
3375+
}
3376+
33613377
if (s == NULL) {
33623378
return NULL;
33633379
}

0 commit comments

Comments
 (0)