Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
f03b4c8a
Commit
f03b4c8a
authored
Aug 11, 2019
by
Raymond Hettinger
Committed by
GitHub
Aug 11, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-37819: Add Fraction.as_integer_ratio() (GH-15212)
parent
09a1872a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
23 additions
and
0 deletions
+23
-0
Doc/library/fractions.rst
Doc/library/fractions.rst
+7
-0
Lib/fractions.py
Lib/fractions.py
+8
-0
Lib/test/test_fractions.py
Lib/test/test_fractions.py
+6
-0
Misc/NEWS.d/next/Library/2019-08-11-10-34-19.bpo-37819.LVJls-.rst
...S.d/next/Library/2019-08-11-10-34-19.bpo-37819.LVJls-.rst
+2
-0
No files found.
Doc/library/fractions.rst
View file @
f03b4c8a
...
...
@@ -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
...
...
Lib/fractions.py
View file @
f03b4c8a
...
...
@@ -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.
...
...
Lib/test/test_fractions.py
View file @
f03b4c8a
...
...
@@ -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
))
...
...
Misc/NEWS.d/next/Library/2019-08-11-10-34-19.bpo-37819.LVJls-.rst
0 → 100644
View file @
f03b4c8a
Add Fraction.as_integer_ratio() to match the corresponding methods in bool,
int, float, and decimal.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment