◐ Shell
clean mode source ↗

[3.6] bpo-31373: remove overly strict float range checks (GH-3486) by benjaminp · Pull Request #3495 · python/cpython

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.6] bpo-31373: remove overly strict float range checks (GH-3486) #3495

Changes from all commits

Commits

File filter

Filter by extension

Conversations

Failed to load comments.

Loading

Jump to

Jump to file

Failed to load files.

Loading

Diff view
Diff view

6 changes: 6 additions & 0 deletions Lib/test/test_float.py

Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,12 @@ def test_float_specials_do_unpack(self):
('<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):
Expand Down

6 changes: 6 additions & 0 deletions Lib/test/test_getargs2.py

Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ def test_f(self):
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)
Expand Down

4 changes: 2 additions & 2 deletions Objects/floatobject.c

Original file line number Diff line number Diff line change
Expand Up @@ -2182,13 +2182,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)
Expand Down

4 changes: 0 additions & 4 deletions Python/getargs.c

Original file line number Diff line number Diff line change
Expand Up @@ -811,10 +811,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;
Expand Down