Skip to content

Commit

Permalink
On path with known exact float, extract the double with the fast macr…
Browse files Browse the repository at this point in the history
…o. (GH-21072)

(cherry picked from commit 930f451)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
  • Loading branch information
miss-islington and rhettinger authored Sep 4, 2020
1 parent e92219d commit 242eac1
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1255,9 +1255,15 @@ static PyObject *
math_floor(PyObject *module, PyObject *number)
/*[clinic end generated code: output=c6a65c4884884b8a input=63af6b5d7ebcc3d6]*/
{
double x;

_Py_IDENTIFIER(__floor__);

if (!PyFloat_CheckExact(number)) {
if (PyFloat_CheckExact(number)) {
x = PyFloat_AS_DOUBLE(number);
}
else
{
PyObject *method = _PyObject_LookupSpecial(number, &PyId___floor__);
if (method != NULL) {
PyObject *result = _PyObject_CallNoArg(method);
Expand All @@ -1266,11 +1272,10 @@ math_floor(PyObject *module, PyObject *number)
}
if (PyErr_Occurred())
return NULL;
x = PyFloat_AsDouble(number);
if (x == -1.0 && PyErr_Occurred())
return NULL;
}
double x = PyFloat_AsDouble(number);
if (x == -1.0 && PyErr_Occurred())
return NULL;

return PyLong_FromDouble(floor(x));
}

Expand Down

0 comments on commit 242eac1

Please sign in to comment.