mirror of
https://github.com/python/cpython.git
synced 2025-10-15 03:10:29 +00:00
Fast path for int inputs to math.dist() and math.hypot() (GH-11692)
This commit is contained in:
parent
ea446409cd
commit
808180c206
2 changed files with 34 additions and 4 deletions
|
@ -766,6 +766,9 @@ class MathTests(unittest.TestCase):
|
||||||
hypot(x=1)
|
hypot(x=1)
|
||||||
with self.assertRaises(TypeError): # Reject values without __float__
|
with self.assertRaises(TypeError): # Reject values without __float__
|
||||||
hypot(1.1, 'string', 2.2)
|
hypot(1.1, 'string', 2.2)
|
||||||
|
int_too_big_for_float = 10 ** (sys.float_info.max_10_exp + 5)
|
||||||
|
with self.assertRaises((ValueError, OverflowError)):
|
||||||
|
hypot(1, int_too_big_for_float)
|
||||||
|
|
||||||
# Any infinity gives positive infinity.
|
# Any infinity gives positive infinity.
|
||||||
self.assertEqual(hypot(INF), INF)
|
self.assertEqual(hypot(INF), INF)
|
||||||
|
@ -805,7 +808,8 @@ class MathTests(unittest.TestCase):
|
||||||
dist = math.dist
|
dist = math.dist
|
||||||
sqrt = math.sqrt
|
sqrt = math.sqrt
|
||||||
|
|
||||||
# Simple exact case
|
# Simple exact cases
|
||||||
|
self.assertEqual(dist((1.0, 2.0, 3.0), (4.0, 2.0, -1.0)), 5.0)
|
||||||
self.assertEqual(dist((1, 2, 3), (4, 2, -1)), 5.0)
|
self.assertEqual(dist((1, 2, 3), (4, 2, -1)), 5.0)
|
||||||
|
|
||||||
# Test different numbers of arguments (from zero to nine)
|
# Test different numbers of arguments (from zero to nine)
|
||||||
|
@ -869,6 +873,11 @@ class MathTests(unittest.TestCase):
|
||||||
dist((1, 2, 3), (4, 5, 6, 7))
|
dist((1, 2, 3), (4, 5, 6, 7))
|
||||||
with self.assertRaises(TypeError): # Rejects invalid types
|
with self.assertRaises(TypeError): # Rejects invalid types
|
||||||
dist("abc", "xyz")
|
dist("abc", "xyz")
|
||||||
|
int_too_big_for_float = 10 ** (sys.float_info.max_10_exp + 5)
|
||||||
|
with self.assertRaises((ValueError, OverflowError)):
|
||||||
|
dist((1, int_too_big_for_float), (2, 3))
|
||||||
|
with self.assertRaises((ValueError, OverflowError)):
|
||||||
|
dist((2, 3), (1, int_too_big_for_float))
|
||||||
|
|
||||||
# Verify that the one dimensional case is equivalent to abs()
|
# Verify that the one dimensional case is equivalent to abs()
|
||||||
for i in range(20):
|
for i in range(20):
|
||||||
|
|
|
@ -2144,7 +2144,14 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
|
||||||
item = PyTuple_GET_ITEM(p, i);
|
item = PyTuple_GET_ITEM(p, i);
|
||||||
if (PyFloat_CheckExact(item)) {
|
if (PyFloat_CheckExact(item)) {
|
||||||
px = PyFloat_AS_DOUBLE(item);
|
px = PyFloat_AS_DOUBLE(item);
|
||||||
} else {
|
}
|
||||||
|
else if (PyLong_CheckExact(item)) {
|
||||||
|
px = PyLong_AsDouble(item);
|
||||||
|
if (px == -1.0 && PyErr_Occurred()) {
|
||||||
|
goto error_exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
px = PyFloat_AsDouble(item);
|
px = PyFloat_AsDouble(item);
|
||||||
if (px == -1.0 && PyErr_Occurred()) {
|
if (px == -1.0 && PyErr_Occurred()) {
|
||||||
goto error_exit;
|
goto error_exit;
|
||||||
|
@ -2153,7 +2160,14 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
|
||||||
item = PyTuple_GET_ITEM(q, i);
|
item = PyTuple_GET_ITEM(q, i);
|
||||||
if (PyFloat_CheckExact(item)) {
|
if (PyFloat_CheckExact(item)) {
|
||||||
qx = PyFloat_AS_DOUBLE(item);
|
qx = PyFloat_AS_DOUBLE(item);
|
||||||
} else {
|
}
|
||||||
|
else if (PyLong_CheckExact(item)) {
|
||||||
|
qx = PyLong_AsDouble(item);
|
||||||
|
if (qx == -1.0 && PyErr_Occurred()) {
|
||||||
|
goto error_exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
qx = PyFloat_AsDouble(item);
|
qx = PyFloat_AsDouble(item);
|
||||||
if (qx == -1.0 && PyErr_Occurred()) {
|
if (qx == -1.0 && PyErr_Occurred()) {
|
||||||
goto error_exit;
|
goto error_exit;
|
||||||
|
@ -2201,7 +2215,14 @@ math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
|
||||||
item = args[i];
|
item = args[i];
|
||||||
if (PyFloat_CheckExact(item)) {
|
if (PyFloat_CheckExact(item)) {
|
||||||
x = PyFloat_AS_DOUBLE(item);
|
x = PyFloat_AS_DOUBLE(item);
|
||||||
} else {
|
}
|
||||||
|
else if (PyLong_CheckExact(item)) {
|
||||||
|
x = PyLong_AsDouble(item);
|
||||||
|
if (x == -1.0 && PyErr_Occurred()) {
|
||||||
|
goto error_exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
x = PyFloat_AsDouble(item);
|
x = PyFloat_AsDouble(item);
|
||||||
if (x == -1.0 && PyErr_Occurred()) {
|
if (x == -1.0 && PyErr_Occurred()) {
|
||||||
goto error_exit;
|
goto error_exit;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue