summaryrefslogtreecommitdiff
path: root/Lib/fractions.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-13 00:19:51 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-13 00:19:51 +0300
commit48e47aaa28d6dfdae128142ffcbc4b0642422e90 (patch)
treef62918798a55edb3aa09e9c95ec92d965fc5283e /Lib/fractions.py
parentf0eeedf0d8ffff9d7ea8874e60992845595f091f (diff)
downloadcpython-git-48e47aaa28d6dfdae128142ffcbc4b0642422e90.tar.gz
Issue #22486: Added the math.gcd() function. The fractions.gcd() function now is
deprecated. Based on patch by Mark Dickinson.
Diffstat (limited to 'Lib/fractions.py')
-rw-r--r--Lib/fractions.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 5ddc84c1a5..60b0728807 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -20,6 +20,17 @@ def gcd(a, b):
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).
"""
+ import warnings
+ warnings.warn('fractions.gcd() is deprecated. Use math.gcd() instead.',
+ DeprecationWarning, 2)
+ if type(a) is int is type(b):
+ if (b or a) < 0:
+ return -math.gcd(a, b)
+ return math.gcd(a, b)
+ return _gcd(a, b)
+
+def _gcd(a, b):
+ # Supports non-integers for backward compatibility.
while b:
a, b = b, a%b
return a
@@ -159,7 +170,7 @@ class Fraction(numbers.Rational):
"or a Rational instance")
elif type(numerator) is int is type(denominator):
- pass # *very* normal case
+ pass # *very* normal case
elif (isinstance(numerator, numbers.Rational) and
isinstance(denominator, numbers.Rational)):
@@ -174,7 +185,13 @@ class Fraction(numbers.Rational):
if denominator == 0:
raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
if _normalize:
- g = gcd(numerator, denominator)
+ if type(numerator) is int is type(denominator):
+ # *very* normal case
+ g = math.gcd(numerator, denominator)
+ if denominator < 0:
+ g = -g
+ else:
+ g = _gcd(numerator, denominator)
numerator //= g
denominator //= g
self._numerator = numerator