mirror of https://github.com/python/cpython.git
bpo-31373: remove overly strict float range checks (#3486)
This undoes a853a8ba78
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.
This commit is contained in:
parent
252033d50e
commit
2bb69a5b4e
|
@ -617,6 +617,12 @@ def test_float_specials_do_unpack(self):
|
||||||
('<f', LE_FLOAT_NAN)]:
|
('<f', LE_FLOAT_NAN)]:
|
||||||
struct.unpack(fmt, data)
|
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):
|
class FormatTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def test_format(self):
|
def test_format(self):
|
||||||
|
|
|
@ -377,6 +377,12 @@ def test_f(self):
|
||||||
r = getargs_f(NAN)
|
r = getargs_f(NAN)
|
||||||
self.assertNotEqual(r, r)
|
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):
|
def test_d(self):
|
||||||
from _testcapi import getargs_d
|
from _testcapi import getargs_d
|
||||||
self.assertEqual(getargs_d(4.25), 4.25)
|
self.assertEqual(getargs_d(4.25), 4.25)
|
||||||
|
|
|
@ -2233,13 +2233,13 @@ _PyFloat_Pack4(double x, unsigned char *p, int le)
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
float y = (float)x;
|
||||||
int i, incr = 1;
|
int i, incr = 1;
|
||||||
|
|
||||||
if (fabs(x) > FLT_MAX && !Py_IS_INFINITY(x))
|
if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
|
||||||
goto Overflow;
|
goto Overflow;
|
||||||
|
|
||||||
unsigned char s[sizeof(float)];
|
unsigned char s[sizeof(float)];
|
||||||
float y = (float)x;
|
|
||||||
memcpy(s, &y, sizeof(float));
|
memcpy(s, &y, sizeof(float));
|
||||||
|
|
||||||
if ((float_format == ieee_little_endian_format && !le)
|
if ((float_format == ieee_little_endian_format && !le)
|
||||||
|
|
|
@ -859,10 +859,6 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
double dval = PyFloat_AsDouble(arg);
|
double dval = PyFloat_AsDouble(arg);
|
||||||
if (PyErr_Occurred())
|
if (PyErr_Occurred())
|
||||||
RETURN_ERR_OCCURRED;
|
RETURN_ERR_OCCURRED;
|
||||||
else if (dval > FLT_MAX)
|
|
||||||
*p = (float)INFINITY;
|
|
||||||
else if (dval < -FLT_MAX)
|
|
||||||
*p = (float)-INFINITY;
|
|
||||||
else
|
else
|
||||||
*p = (float) dval;
|
*p = (float) dval;
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue