Skip to content

Commit 808180c

Browse files
authored
Fast path for int inputs to math.dist() and math.hypot() (GH-11692)
1 parent ea44640 commit 808180c

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

Lib/test/test_math.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,9 @@ def testHypot(self):
766766
hypot(x=1)
767767
with self.assertRaises(TypeError): # Reject values without __float__
768768
hypot(1.1, 'string', 2.2)
769+
int_too_big_for_float = 10 ** (sys.float_info.max_10_exp + 5)
770+
with self.assertRaises((ValueError, OverflowError)):
771+
hypot(1, int_too_big_for_float)
769772

770773
# Any infinity gives positive infinity.
771774
self.assertEqual(hypot(INF), INF)
@@ -805,7 +808,8 @@ def testDist(self):
805808
dist = math.dist
806809
sqrt = math.sqrt
807810

808-
# Simple exact case
811+
# Simple exact cases
812+
self.assertEqual(dist((1.0, 2.0, 3.0), (4.0, 2.0, -1.0)), 5.0)
809813
self.assertEqual(dist((1, 2, 3), (4, 2, -1)), 5.0)
810814

811815
# Test different numbers of arguments (from zero to nine)
@@ -869,6 +873,11 @@ class T(tuple):
869873
dist((1, 2, 3), (4, 5, 6, 7))
870874
with self.assertRaises(TypeError): # Rejects invalid types
871875
dist("abc", "xyz")
876+
int_too_big_for_float = 10 ** (sys.float_info.max_10_exp + 5)
877+
with self.assertRaises((ValueError, OverflowError)):
878+
dist((1, int_too_big_for_float), (2, 3))
879+
with self.assertRaises((ValueError, OverflowError)):
880+
dist((2, 3), (1, int_too_big_for_float))
872881

873882
# Verify that the one dimensional case is equivalent to abs()
874883
for i in range(20):

Modules/mathmodule.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,7 +2144,14 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
21442144
item = PyTuple_GET_ITEM(p, i);
21452145
if (PyFloat_CheckExact(item)) {
21462146
px = PyFloat_AS_DOUBLE(item);
2147-
} else {
2147+
}
2148+
else if (PyLong_CheckExact(item)) {
2149+
px = PyLong_AsDouble(item);
2150+
if (px == -1.0 && PyErr_Occurred()) {
2151+
goto error_exit;
2152+
}
2153+
}
2154+
else {
21482155
px = PyFloat_AsDouble(item);
21492156
if (px == -1.0 && PyErr_Occurred()) {
21502157
goto error_exit;
@@ -2153,7 +2160,14 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
21532160
item = PyTuple_GET_ITEM(q, i);
21542161
if (PyFloat_CheckExact(item)) {
21552162
qx = PyFloat_AS_DOUBLE(item);
2156-
} else {
2163+
}
2164+
else if (PyLong_CheckExact(item)) {
2165+
qx = PyLong_AsDouble(item);
2166+
if (qx == -1.0 && PyErr_Occurred()) {
2167+
goto error_exit;
2168+
}
2169+
}
2170+
else {
21572171
qx = PyFloat_AsDouble(item);
21582172
if (qx == -1.0 && PyErr_Occurred()) {
21592173
goto error_exit;
@@ -2201,7 +2215,14 @@ math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
22012215
item = args[i];
22022216
if (PyFloat_CheckExact(item)) {
22032217
x = PyFloat_AS_DOUBLE(item);
2204-
} else {
2218+
}
2219+
else if (PyLong_CheckExact(item)) {
2220+
x = PyLong_AsDouble(item);
2221+
if (x == -1.0 && PyErr_Occurred()) {
2222+
goto error_exit;
2223+
}
2224+
}
2225+
else {
22052226
x = PyFloat_AsDouble(item);
22062227
if (x == -1.0 && PyErr_Occurred()) {
22072228
goto error_exit;

0 commit comments

Comments
 (0)