Skip to content

Commit

Permalink
pythongh-101825: Clarify that as_integer_ratio() output is always nor…
Browse files Browse the repository at this point in the history
…malized (python#101843)

Make docstrings for `as_integer_ratio` consistent across types, and document that
the returned pair is always normalized (coprime integers, with positive denominator).

---------

Co-authored-by: Owain Davies <[email protected]>
Co-authored-by: Mark Dickinson <[email protected]>
  • Loading branch information
3 people authored Feb 27, 2023
1 parent 4f3786b commit 4624987
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Doc/library/fractions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ another rational number, or from a string.
.. method:: as_integer_ratio()

Return a tuple of two integers, whose ratio is equal
to the Fraction and with a positive denominator.
to the original Fraction. The ratio is in lowest terms
and has a positive denominator.

.. versionadded:: 3.8

Expand Down
6 changes: 3 additions & 3 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,8 @@ class`. In addition, it provides a few more methods:

.. method:: int.as_integer_ratio()

Return a pair of integers whose ratio is exactly equal to the original
integer and with a positive denominator. The integer ratio of integers
Return a pair of integers whose ratio is equal to the original
integer and has a positive denominator. The integer ratio of integers
(whole numbers) is always the integer as the numerator and ``1`` as the
denominator.

Expand All @@ -624,7 +624,7 @@ class`. float also has the following additional methods.
.. method:: float.as_integer_ratio()

Return a pair of integers whose ratio is exactly equal to the
original float and with a positive denominator. Raises
original float. The ratio is in lowest terms and has a positive denominator. Raises
:exc:`OverflowError` on infinities and a :exc:`ValueError` on
NaNs.

Expand Down
5 changes: 2 additions & 3 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,9 @@ def is_integer(self):
return self._denominator == 1

def as_integer_ratio(self):
"""Return the integer ratio as a tuple.
"""Return a pair of integers, whose ratio is equal to the original Fraction.
Return a tuple of two integers, whose ratio is equal to the
Fraction and with a positive denominator.
The ratio is in lowest terms and has a positive denominator.
"""
return (self._numerator, self._denominator)

Expand Down
10 changes: 4 additions & 6 deletions Objects/clinic/floatobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Objects/clinic/longobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1546,12 +1546,10 @@ float_fromhex(PyTypeObject *type, PyObject *string)
/*[clinic input]
float.as_integer_ratio
Return integer ratio.
Return a pair of integers, whose ratio is exactly equal to the original float.
Return a pair of integers, whose ratio is exactly equal to the original float
and with a positive denominator.
Raise OverflowError on infinities and a ValueError on NaNs.
The ratio is in lowest terms and has a positive denominator. Raise
OverflowError on infinities and a ValueError on NaNs.
>>> (10.0).as_integer_ratio()
(10, 1)
Expand All @@ -1563,7 +1561,7 @@ Raise OverflowError on infinities and a ValueError on NaNs.

static PyObject *
float_as_integer_ratio_impl(PyObject *self)
/*[clinic end generated code: output=65f25f0d8d30a712 input=e21d08b4630c2e44]*/
/*[clinic end generated code: output=65f25f0d8d30a712 input=d5ba7765655d75bd]*/
{
double self_double;
double float_part;
Expand Down
7 changes: 3 additions & 4 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6020,10 +6020,9 @@ int_bit_count_impl(PyObject *self)
/*[clinic input]
int.as_integer_ratio
Return integer ratio.
Return a pair of integers, whose ratio is equal to the original int.
Return a pair of integers, whose ratio is exactly equal to the original int
and with a positive denominator.
The ratio is in lowest terms and has a positive denominator.
>>> (10).as_integer_ratio()
(10, 1)
Expand All @@ -6035,7 +6034,7 @@ and with a positive denominator.

static PyObject *
int_as_integer_ratio_impl(PyObject *self)
/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/
/*[clinic end generated code: output=e60803ae1cc8621a input=384ff1766634bec2]*/
{
PyObject *ratio_tuple;
PyObject *numerator = long_long(self);
Expand Down

0 comments on commit 4624987

Please sign in to comment.