mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Clean up float.as_integer_ratio().
This commit is contained in:
parent
0d250bc119
commit
4e6aad1f7a
1 changed files with 16 additions and 20 deletions
|
@ -1451,18 +1451,12 @@ float_as_integer_ratio(PyObject *v, PyObject *unused)
|
||||||
int exponent;
|
int exponent;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
PyObject *prev;
|
|
||||||
PyObject *py_exponent = NULL;
|
PyObject *py_exponent = NULL;
|
||||||
PyObject *numerator = NULL;
|
PyObject *numerator = NULL;
|
||||||
PyObject *denominator = NULL;
|
PyObject *denominator = NULL;
|
||||||
PyObject *result_pair = NULL;
|
PyObject *result_pair = NULL;
|
||||||
PyNumberMethods *long_methods = PyLong_Type.tp_as_number;
|
PyNumberMethods *long_methods = PyLong_Type.tp_as_number;
|
||||||
|
|
||||||
#define INPLACE_UPDATE(obj, call) \
|
|
||||||
prev = obj; \
|
|
||||||
obj = call; \
|
|
||||||
Py_DECREF(prev); \
|
|
||||||
|
|
||||||
CONVERT_TO_DOUBLE(v, self);
|
CONVERT_TO_DOUBLE(v, self);
|
||||||
|
|
||||||
if (Py_IS_INFINITY(self)) {
|
if (Py_IS_INFINITY(self)) {
|
||||||
|
@ -1489,29 +1483,31 @@ float_as_integer_ratio(PyObject *v, PyObject *unused)
|
||||||
to be truncated by PyLong_FromDouble(). */
|
to be truncated by PyLong_FromDouble(). */
|
||||||
|
|
||||||
numerator = PyLong_FromDouble(float_part);
|
numerator = PyLong_FromDouble(float_part);
|
||||||
if (numerator == NULL) goto error;
|
if (numerator == NULL)
|
||||||
|
goto error;
|
||||||
|
denominator = PyLong_FromLong(1);
|
||||||
|
if (denominator == NULL)
|
||||||
|
goto error;
|
||||||
|
py_exponent = PyLong_FromLong(Py_ABS(exponent));
|
||||||
|
if (py_exponent == NULL)
|
||||||
|
goto error;
|
||||||
|
|
||||||
/* fold in 2**exponent */
|
/* fold in 2**exponent */
|
||||||
denominator = PyLong_FromLong(1);
|
|
||||||
py_exponent = PyLong_FromLong(labs((long)exponent));
|
|
||||||
if (py_exponent == NULL) goto error;
|
|
||||||
INPLACE_UPDATE(py_exponent,
|
|
||||||
long_methods->nb_lshift(denominator, py_exponent));
|
|
||||||
if (py_exponent == NULL) goto error;
|
|
||||||
if (exponent > 0) {
|
if (exponent > 0) {
|
||||||
INPLACE_UPDATE(numerator,
|
Py_SETREF(numerator,
|
||||||
long_methods->nb_multiply(numerator, py_exponent));
|
long_methods->nb_lshift(numerator, py_exponent));
|
||||||
if (numerator == NULL) goto error;
|
if (numerator == NULL)
|
||||||
|
goto error;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Py_DECREF(denominator);
|
Py_SETREF(denominator,
|
||||||
denominator = py_exponent;
|
long_methods->nb_lshift(denominator, py_exponent));
|
||||||
py_exponent = NULL;
|
if (denominator == NULL)
|
||||||
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
result_pair = PyTuple_Pack(2, numerator, denominator);
|
result_pair = PyTuple_Pack(2, numerator, denominator);
|
||||||
|
|
||||||
#undef INPLACE_UPDATE
|
|
||||||
error:
|
error:
|
||||||
Py_XDECREF(py_exponent);
|
Py_XDECREF(py_exponent);
|
||||||
Py_XDECREF(denominator);
|
Py_XDECREF(denominator);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue