summaryrefslogtreecommitdiffstats
path: root/Modules/mathmodule.c
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2018-08-27 00:38:31 (GMT)
committerGitHub <noreply@github.com>2018-08-27 00:38:31 (GMT)
commit74734f73ca3cdb48d4d51139512b6828c2734252 (patch)
tree82548bccb9e8c5384c434f93b02db29f33432076 /Modules/mathmodule.c
parent89d79b1449750b14ded0149dcdd1e39247f2c65d (diff)
downloadcpython-74734f73ca3cdb48d4d51139512b6828c2734252.zip
cpython-74734f73ca3cdb48d4d51139512b6828c2734252.tar.gz
cpython-74734f73ca3cdb48d4d51139512b6828c2734252.tar.bz2
Fast path for exact floats in math.hypot() and math.dist() (GH-8949)
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r--Modules/mathmodule.c30
1 files changed, 21 insertions, 9 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 2d483af..62d3279 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -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;