Skip to content

Commit cb356c2

Browse files
authored
[3.6] bpo-31373: remove overly strict float range checks (GH-3486) (#3495)
This undoes a853a8b except for the pytime.c parts. We want to continue to allow IEEE 754 doubles larger than FLT_MAX to be rounded into finite floats. Tests were added to very this behavior. (cherry picked from commit 2bb69a5)
1 parent 99b941b commit cb356c2

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

Lib/test/test_float.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,12 @@ def test_float_specials_do_unpack(self):
613613
('<f', LE_FLOAT_NAN)]:
614614
struct.unpack(fmt, data)
615615

616+
@support.requires_IEEE_754
617+
def test_serialized_float_rounding(self):
618+
from _testcapi import FLT_MAX
619+
self.assertEqual(struct.pack("<f", 3.40282356e38), struct.pack("<f", FLT_MAX))
620+
self.assertEqual(struct.pack("<f", -3.40282356e38), struct.pack("<f", -FLT_MAX))
621+
616622
class FormatTestCase(unittest.TestCase):
617623

618624
def test_format(self):

Lib/test/test_getargs2.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ def test_f(self):
377377
r = getargs_f(NAN)
378378
self.assertNotEqual(r, r)
379379

380+
@support.requires_IEEE_754
381+
def test_f_rounding(self):
382+
from _testcapi import getargs_f
383+
self.assertEqual(getargs_f(3.40282356e38), FLT_MAX)
384+
self.assertEqual(getargs_f(-3.40282356e38), -FLT_MAX)
385+
380386
def test_d(self):
381387
from _testcapi import getargs_d
382388
self.assertEqual(getargs_d(4.25), 4.25)

Objects/floatobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,13 +2182,13 @@ _PyFloat_Pack4(double x, unsigned char *p, int le)
21822182

21832183
}
21842184
else {
2185+
float y = (float)x;
21852186
int i, incr = 1;
21862187

2187-
if (fabs(x) > FLT_MAX && !Py_IS_INFINITY(x))
2188+
if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
21882189
goto Overflow;
21892190

21902191
unsigned char s[sizeof(float)];
2191-
float y = (float)x;
21922192
memcpy(s, &y, sizeof(float));
21932193

21942194
if ((float_format == ieee_little_endian_format && !le)

Python/getargs.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -811,10 +811,6 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
811811
double dval = PyFloat_AsDouble(arg);
812812
if (PyErr_Occurred())
813813
RETURN_ERR_OCCURRED;
814-
else if (dval > FLT_MAX)
815-
*p = (float)INFINITY;
816-
else if (dval < -FLT_MAX)
817-
*p = (float)-INFINITY;
818814
else
819815
*p = (float) dval;
820816
break;

0 commit comments

Comments
 (0)