Skip to content

Commit 242eac1

Browse files
On path with known exact float, extract the double with the fast macro. (GH-21072)
(cherry picked from commit 930f451) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
1 parent e92219d commit 242eac1

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Modules/mathmodule.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,9 +1255,15 @@ static PyObject *
12551255
math_floor(PyObject *module, PyObject *number)
12561256
/*[clinic end generated code: output=c6a65c4884884b8a input=63af6b5d7ebcc3d6]*/
12571257
{
1258+
double x;
1259+
12581260
_Py_IDENTIFIER(__floor__);
12591261

1260-
if (!PyFloat_CheckExact(number)) {
1262+
if (PyFloat_CheckExact(number)) {
1263+
x = PyFloat_AS_DOUBLE(number);
1264+
}
1265+
else
1266+
{
12611267
PyObject *method = _PyObject_LookupSpecial(number, &PyId___floor__);
12621268
if (method != NULL) {
12631269
PyObject *result = _PyObject_CallNoArg(method);
@@ -1266,11 +1272,10 @@ math_floor(PyObject *module, PyObject *number)
12661272
}
12671273
if (PyErr_Occurred())
12681274
return NULL;
1275+
x = PyFloat_AsDouble(number);
1276+
if (x == -1.0 && PyErr_Occurred())
1277+
return NULL;
12691278
}
1270-
double x = PyFloat_AsDouble(number);
1271-
if (x == -1.0 && PyErr_Occurred())
1272-
return NULL;
1273-
12741279
return PyLong_FromDouble(floor(x));
12751280
}
12761281

0 commit comments

Comments
 (0)