Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Optimized :func:`math.floor()`, :func:`math.ceil()` and :func:`math.trunc()`
for floats.
32 changes: 20 additions & 12 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1114,15 +1114,17 @@ math_ceil(PyObject *module, PyObject *number)
_Py_IDENTIFIER(__ceil__);
PyObject *method, *result;

method = _PyObject_LookupSpecial(number, &PyId___ceil__);
if (method == NULL) {
if (!PyFloat_CheckExact(number)) {
method = _PyObject_LookupSpecial(number, &PyId___ceil__);
if (method != NULL) {
result = _PyObject_CallNoArg(method);
Py_DECREF(method);
return result;
}
if (PyErr_Occurred())
return NULL;
return math_1_to_int(number, ceil, 0);
}
result = _PyObject_CallNoArg(method);
Py_DECREF(method);
return result;
return math_1_to_int(number, ceil, 0);
}

FUNC2(copysign, copysign,
Expand Down Expand Up @@ -1172,15 +1174,17 @@ math_floor(PyObject *module, PyObject *number)
_Py_IDENTIFIER(__floor__);
PyObject *method, *result;

method = _PyObject_LookupSpecial(number, &PyId___floor__);
if (method == NULL) {
if (!PyFloat_CheckExact(number)) {
method = _PyObject_LookupSpecial(number, &PyId___floor__);
if (method != NULL) {
result = _PyObject_CallNoArg(method);
Py_DECREF(method);
return result;
}
if (PyErr_Occurred())
return NULL;
return math_1_to_int(number, floor, 0);
}
result = _PyObject_CallNoArg(method);
Py_DECREF(method);
return result;
return math_1_to_int(number, floor, 0);
}

FUNC1A(gamma, m_tgamma,
Expand Down Expand Up @@ -2061,6 +2065,10 @@ math_trunc(PyObject *module, PyObject *x)
_Py_IDENTIFIER(__trunc__);
PyObject *trunc, *result;

if (PyFloat_CheckExact(x)) {
return PyFloat_Type.tp_as_number->nb_int(x);
}

if (Py_TYPE(x)->tp_dict == NULL) {
if (PyType_Ready(Py_TYPE(x)) < 0)
return NULL;
Expand Down