summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2019-08-11 21:40:59 (GMT)
committerGitHub <noreply@github.com>2019-08-11 21:40:59 (GMT)
commitf03b4c8a48f62134799d368b78da35301af466a3 (patch)
tree43a5a1282521e4164e63e05674dc7830319700ec
parent09a1872a8007048dcdf825a476816c5e3498b8f8 (diff)
downloadcpython-f03b4c8a48f62134799d368b78da35301af466a3.zip
cpython-f03b4c8a48f62134799d368b78da35301af466a3.tar.gz
cpython-f03b4c8a48f62134799d368b78da35301af466a3.tar.bz2
bpo-37819: Add Fraction.as_integer_ratio() (GH-15212)
-rw-r--r--Doc/library/fractions.rst7
-rw-r--r--Lib/fractions.py8
-rw-r--r--Lib/test/test_fractions.py6
-rw-r--r--Misc/NEWS.d/next/Library/2019-08-11-10-34-19.bpo-37819.LVJls-.rst2
4 files changed, 23 insertions, 0 deletions
diff --git a/Doc/library/fractions.rst b/Doc/library/fractions.rst
index b5a818e..58e7126 100644
--- a/Doc/library/fractions.rst
+++ b/Doc/library/fractions.rst
@@ -94,6 +94,13 @@ another rational number, or from a string.
Denominator of the Fraction in lowest term.
+ .. method:: as_integer_ratio()
+
+ Return a tuple of two integers, whose ratio is equal
+ to the Fraction and with a positive denominator.
+
+ .. versionadded:: 3.8
+
.. method:: from_float(flt)
This class method constructs a :class:`Fraction` representing the exact
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 7443bd3..e774d58 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -216,6 +216,14 @@ class Fraction(numbers.Rational):
(cls.__name__, dec, type(dec).__name__))
return cls(*dec.as_integer_ratio())
+ def as_integer_ratio(self):
+ """Return the integer ratio as a tuple.
+
+ Return a tuple of two integers, whose ratio is equal to the
+ Fraction and with a positive denominator.
+ """
+ return (self._numerator, self._denominator)
+
def limit_denominator(self, max_denominator=1000000):
"""Closest Fraction to self with denominator at most max_denominator.
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index 2779162..18ab28c 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -302,6 +302,12 @@ class FractionTest(unittest.TestCase):
ValueError, "cannot convert NaN to integer ratio",
F.from_decimal, Decimal("snan"))
+ def test_as_integer_ratio(self):
+ self.assertEqual(F(4, 6).as_integer_ratio(), (2, 3))
+ self.assertEqual(F(-4, 6).as_integer_ratio(), (-2, 3))
+ self.assertEqual(F(4, -6).as_integer_ratio(), (-2, 3))
+ self.assertEqual(F(0, 6).as_integer_ratio(), (0, 1))
+
def testLimitDenominator(self):
rpi = F('3.1415926535897932')
self.assertEqual(rpi.limit_denominator(10000), F(355, 113))
diff --git a/Misc/NEWS.d/next/Library/2019-08-11-10-34-19.bpo-37819.LVJls-.rst b/Misc/NEWS.d/next/Library/2019-08-11-10-34-19.bpo-37819.LVJls-.rst
new file mode 100644
index 0000000..cfc1f1a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-08-11-10-34-19.bpo-37819.LVJls-.rst
@@ -0,0 +1,2 @@
+Add Fraction.as_integer_ratio() to match the corresponding methods in bool,
+int, float, and decimal.