summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2019-01-28 21:59:56 (GMT)
committerGitHub <noreply@github.com>2019-01-28 21:59:56 (GMT)
commit808180c206fbde390d9dbdf24a8989fc8a6446ec (patch)
tree2e433b434572696ff09dcfa9c78bb84eef5054b3 /Modules
parentea446409cd5f1364beafd5e5255da6799993f285 (diff)
downloadcpython-808180c206fbde390d9dbdf24a8989fc8a6446ec.zip
cpython-808180c206fbde390d9dbdf24a8989fc8a6446ec.tar.gz
cpython-808180c206fbde390d9dbdf24a8989fc8a6446ec.tar.bz2
Fast path for int inputs to math.dist() and math.hypot() (GH-11692)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/mathmodule.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index a190f5c..c435377 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -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;