Skip to content

Commit 4624987

Browse files
skirpichevOTheDevmdickinson
authored
gh-101825: Clarify that as_integer_ratio() output is always normalized (#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]>
1 parent 4f3786b commit 4624987

File tree

7 files changed

+21
-27
lines changed

7 files changed

+21
-27
lines changed

Doc/library/fractions.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ another rational number, or from a string.
118118
.. method:: as_integer_ratio()
119119

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

123124
.. versionadded:: 3.8
124125

Doc/library/stdtypes.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,8 @@ class`. In addition, it provides a few more methods:
602602

603603
.. method:: int.as_integer_ratio()
604604

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

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

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

Lib/fractions.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,9 @@ def is_integer(self):
331331
return self._denominator == 1
332332

333333
def as_integer_ratio(self):
334-
"""Return the integer ratio as a tuple.
334+
"""Return a pair of integers, whose ratio is equal to the original Fraction.
335335
336-
Return a tuple of two integers, whose ratio is equal to the
337-
Fraction and with a positive denominator.
336+
The ratio is in lowest terms and has a positive denominator.
338337
"""
339338
return (self._numerator, self._denominator)
340339

Objects/clinic/floatobject.c.h

+4-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/clinic/longobject.c.h

+3-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/floatobject.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -1546,12 +1546,10 @@ float_fromhex(PyTypeObject *type, PyObject *string)
15461546
/*[clinic input]
15471547
float.as_integer_ratio
15481548
1549-
Return integer ratio.
1549+
Return a pair of integers, whose ratio is exactly equal to the original float.
15501550
1551-
Return a pair of integers, whose ratio is exactly equal to the original float
1552-
and with a positive denominator.
1553-
1554-
Raise OverflowError on infinities and a ValueError on NaNs.
1551+
The ratio is in lowest terms and has a positive denominator. Raise
1552+
OverflowError on infinities and a ValueError on NaNs.
15551553
15561554
>>> (10.0).as_integer_ratio()
15571555
(10, 1)
@@ -1563,7 +1561,7 @@ Raise OverflowError on infinities and a ValueError on NaNs.
15631561

15641562
static PyObject *
15651563
float_as_integer_ratio_impl(PyObject *self)
1566-
/*[clinic end generated code: output=65f25f0d8d30a712 input=e21d08b4630c2e44]*/
1564+
/*[clinic end generated code: output=65f25f0d8d30a712 input=d5ba7765655d75bd]*/
15671565
{
15681566
double self_double;
15691567
double float_part;

Objects/longobject.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -6020,10 +6020,9 @@ int_bit_count_impl(PyObject *self)
60206020
/*[clinic input]
60216021
int.as_integer_ratio
60226022
6023-
Return integer ratio.
6023+
Return a pair of integers, whose ratio is equal to the original int.
60246024
6025-
Return a pair of integers, whose ratio is exactly equal to the original int
6026-
and with a positive denominator.
6025+
The ratio is in lowest terms and has a positive denominator.
60276026
60286027
>>> (10).as_integer_ratio()
60296028
(10, 1)
@@ -6035,7 +6034,7 @@ and with a positive denominator.
60356034

60366035
static PyObject *
60376036
int_as_integer_ratio_impl(PyObject *self)
6038-
/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/
6037+
/*[clinic end generated code: output=e60803ae1cc8621a input=384ff1766634bec2]*/
60396038
{
60406039
PyObject *ratio_tuple;
60416040
PyObject *numerator = long_long(self);

0 commit comments

Comments
 (0)