mirror of
https://github.com/python/cpython
synced 2024-07-21 10:15:53 +00:00
gh-110489: Optimise math.ceil for known exact float (#108801)
This matches a similar optimisation done for math.floor in https://github.com/python/cpython/pull/21072
This commit is contained in:
parent
201dc11aeb
commit
f013b47504
|
@ -0,0 +1 @@
|
|||
Optimise :func:`math.ceil` when the input is exactly a float, resulting in about a 10% improvement.
|
|
@ -1125,8 +1125,12 @@ static PyObject *
|
|||
math_ceil(PyObject *module, PyObject *number)
|
||||
/*[clinic end generated code: output=6c3b8a78bc201c67 input=2725352806399cab]*/
|
||||
{
|
||||
double x;
|
||||
|
||||
if (!PyFloat_CheckExact(number)) {
|
||||
if (PyFloat_CheckExact(number)) {
|
||||
x = PyFloat_AS_DOUBLE(number);
|
||||
}
|
||||
else {
|
||||
math_module_state *state = get_math_module_state(module);
|
||||
PyObject *method = _PyObject_LookupSpecial(number, state->str___ceil__);
|
||||
if (method != NULL) {
|
||||
|
@ -1136,11 +1140,10 @@ math_ceil(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(ceil(x));
|
||||
}
|
||||
|
||||
|
@ -1196,8 +1199,7 @@ math_floor(PyObject *module, PyObject *number)
|
|||
if (PyFloat_CheckExact(number)) {
|
||||
x = PyFloat_AS_DOUBLE(number);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
math_module_state *state = get_math_module_state(module);
|
||||
PyObject *method = _PyObject_LookupSpecial(number, state->str___floor__);
|
||||
if (method != NULL) {
|
||||
|
|
Loading…
Reference in a new issue