Fast path for int inputs to math.dist() and math.hypot() (GH-11692)

This commit is contained in:
Raymond Hettinger 2019-01-28 13:59:56 -08:00 committed by GitHub
parent ea446409cd
commit 808180c206
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 4 deletions

View file

@ -2144,7 +2144,14 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
item = PyTuple_GET_ITEM(p, i);
if (PyFloat_CheckExact(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);
if (px == -1.0 && PyErr_Occurred()) {
goto error_exit;
@ -2153,7 +2160,14 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
item = PyTuple_GET_ITEM(q, i);
if (PyFloat_CheckExact(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);
if (qx == -1.0 && PyErr_Occurred()) {
goto error_exit;
@ -2201,7 +2215,14 @@ math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
item = args[i];
if (PyFloat_CheckExact(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);
if (x == -1.0 && PyErr_Occurred()) {
goto error_exit;