]> granicus.if.org Git - python/commitdiff
bpo-31373: remove overly strict float range checks (#3486)
authorBenjamin Peterson <benjamin@python.org>
Mon, 11 Sep 2017 06:50:46 +0000 (23:50 -0700)
committerGitHub <noreply@github.com>
Mon, 11 Sep 2017 06:50:46 +0000 (23:50 -0700)
This undoes a853a8ba7850381d49b284295dd6f0dc491dbe44 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.

Lib/test/test_float.py
Lib/test/test_getargs2.py
Objects/floatobject.c
Python/getargs.c

index 66726d6496d600fe274d20a8eb9a88625a358900..a16c05cf5d99c5c81dda6092dea2aca5fc6a6561 100644 (file)
@@ -617,6 +617,12 @@ class IEEEFormatTestCase(unittest.TestCase):
                           ('<f', LE_FLOAT_NAN)]:
             struct.unpack(fmt, data)
 
+    @support.requires_IEEE_754
+    def test_serialized_float_rounding(self):
+        from _testcapi import FLT_MAX
+        self.assertEqual(struct.pack("<f", 3.40282356e38), struct.pack("<f", FLT_MAX))
+        self.assertEqual(struct.pack("<f", -3.40282356e38), struct.pack("<f", -FLT_MAX))
+
 class FormatTestCase(unittest.TestCase):
 
     def test_format(self):
index 86df3a475042ca9a692e4dff4d16d273ccf5b2f4..20f6f4d8ad35e24bcc0086ca3498d106ac6329d2 100644 (file)
@@ -377,6 +377,12 @@ class Float_TestCase(unittest.TestCase):
         r = getargs_f(NAN)
         self.assertNotEqual(r, r)
 
+    @support.requires_IEEE_754
+    def test_f_rounding(self):
+        from _testcapi import getargs_f
+        self.assertEqual(getargs_f(3.40282356e38), FLT_MAX)
+        self.assertEqual(getargs_f(-3.40282356e38), -FLT_MAX)
+
     def test_d(self):
         from _testcapi import getargs_d
         self.assertEqual(getargs_d(4.25), 4.25)
index fc1ddf40816e2f65b441c621e0f41b913201f9a6..8d7a55ac6f6401c043da54e1899f762846b58865 100644 (file)
@@ -2233,13 +2233,13 @@ _PyFloat_Pack4(double x, unsigned char *p, int le)
 
     }
     else {
+        float y = (float)x;
         int i, incr = 1;
 
-        if (fabs(x) > FLT_MAX && !Py_IS_INFINITY(x))
+        if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
             goto Overflow;
 
         unsigned char s[sizeof(float)];
-        float y = (float)x;
         memcpy(s, &y, sizeof(float));
 
         if ((float_format == ieee_little_endian_format && !le)
index 0b155a170f90855e2ee98fff42338fbfe80582ae..dd7ca9fed15ed2c762166e90c4d8d8022c353f05 100644 (file)
@@ -859,10 +859,6 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
         double dval = PyFloat_AsDouble(arg);
         if (PyErr_Occurred())
             RETURN_ERR_OCCURRED;
-        else if (dval > FLT_MAX)
-            *p = (float)INFINITY;
-        else if (dval < -FLT_MAX)
-            *p = (float)-INFINITY;
         else
             *p = (float) dval;
         break;