]> granicus.if.org Git - python/commitdiff
Fast path for exact floats in math.hypot() and math.dist() (GH-8949)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Mon, 27 Aug 2018 00:38:31 +0000 (19:38 -0500)
committerGitHub <noreply@github.com>
Mon, 27 Aug 2018 00:38:31 +0000 (19:38 -0500)
Modules/mathmodule.c

index 2d483afff54d1b38cc4667e2c7511b680fc4f784..62d327998fd183d8b2a2042270dd361291dc422d 100644 (file)
@@ -2134,14 +2134,22 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
     }
     for (i=0 ; i<n ; i++) {
         item = PyTuple_GET_ITEM(p, i);
-        px = PyFloat_AsDouble(item);
-        if (px == -1.0 && PyErr_Occurred()) {
-            goto error_exit;
+        if (PyFloat_CheckExact(item)) {
+            px = PyFloat_AS_DOUBLE(item);
+        } else {
+            px = PyFloat_AsDouble(item);
+            if (px == -1.0 && PyErr_Occurred()) {
+                goto error_exit;
+            }
         }
         item = PyTuple_GET_ITEM(q, i);
-        qx = PyFloat_AsDouble(item);
-        if (qx == -1.0 && PyErr_Occurred()) {
-            goto error_exit;
+        if (PyFloat_CheckExact(item)) {
+            qx = PyFloat_AS_DOUBLE(item);
+        } else {
+            qx = PyFloat_AsDouble(item);
+            if (qx == -1.0 && PyErr_Occurred()) {
+                goto error_exit;
+            }
         }
         x = fabs(px - qx);
         diffs[i] = x;
@@ -2183,9 +2191,13 @@ math_hypot(PyObject *self, PyObject *args)
     }
     for (i=0 ; i<n ; i++) {
         item = PyTuple_GET_ITEM(args, i);
-        x = PyFloat_AsDouble(item);
-        if (x == -1.0 && PyErr_Occurred()) {
-            goto error_exit;
+        if (PyFloat_CheckExact(item)) {
+            x = PyFloat_AS_DOUBLE(item);
+        } else {
+            x = PyFloat_AsDouble(item);
+            if (x == -1.0 && PyErr_Occurred()) {
+                goto error_exit;
+            }
         }
         x = fabs(x);
         coordinates[i] = x;