bpo-33073: Adding as_integer_ratio to ints. (GH-8750)

This commit is contained in:
Lisa Roach 2018-09-13 23:56:23 -07:00 committed by Raymond Hettinger
parent 83df50ea57
commit 5ac704306f
8 changed files with 108 additions and 2 deletions

View file

@ -5260,6 +5260,36 @@ long_is_finite(PyObject *v)
}
#endif
/*[clinic input]
int.as_integer_ratio
Return integer ratio.
Return a pair of integers, whose ratio is exactly equal to the original int
and with a positive denominator.
>>> (10).as_integer_ratio()
(10, 1)
>>> (-10).as_integer_ratio()
(-10, 1)
>>> (0).as_integer_ratio()
(0, 1)
[clinic start generated code]*/
static PyObject *
int_as_integer_ratio_impl(PyObject *self)
/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/
{
if PyLong_CheckExact(self) {
return PyTuple_Pack(2, self, _PyLong_One);
} else {
PyObject *numerator = _PyLong_Copy(self);
PyObject *ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One);
Py_DECREF(numerator);
return ratio_tuple;
}
}
/*[clinic input]
int.to_bytes
@ -5392,6 +5422,7 @@ static PyMethodDef long_methods[] = {
#endif
INT_TO_BYTES_METHODDEF
INT_FROM_BYTES_METHODDEF
INT_AS_INTEGER_RATIO_METHODDEF
{"__trunc__", long_long_meth, METH_NOARGS,
"Truncating an Integral returns itself."},
{"__floor__", long_long_meth, METH_NOARGS,