Author: Armin Rigo <ar...@tunes.org>
Branch: py3.5
Changeset: r87099:682481164902
Date: 2016-09-13 20:37 +0200
http://bitbucket.org/pypy/pypy/changeset/682481164902/

Log:    math.gcd()

diff --git a/pypy/module/math/__init__.py b/pypy/module/math/__init__.py
--- a/pypy/module/math/__init__.py
+++ b/pypy/module/math/__init__.py
@@ -4,7 +4,8 @@
 
 class Module(MixedModule):
     appleveldefs = {
-       'factorial' : 'app_math.factorial'
+       'factorial' : 'app_math.factorial',
+       'gcd' :       'app_math.gcd',
     }
 
     interpleveldefs = {
diff --git a/pypy/module/math/app_math.py b/pypy/module/math/app_math.py
--- a/pypy/module/math/app_math.py
+++ b/pypy/module/math/app_math.py
@@ -42,3 +42,11 @@
 
     res, _, shift = _fac1(x)
     return res << shift
+
+def gcd(x, y):
+    """greatest common divisor of x and y"""
+    x = abs(x)
+    y = abs(y)
+    while x > 0:
+        x, y = y % x, x
+    return y
diff --git a/pypy/module/math/test/test_math.py 
b/pypy/module/math/test/test_math.py
--- a/pypy/module/math/test/test_math.py
+++ b/pypy/module/math/test/test_math.py
@@ -363,3 +363,9 @@
         #
         raises(TypeError, math.isclose, 0, 1, rel_tol=None)
         raises(TypeError, math.isclose, 0, 1, abs_tol=None)
+
+    def test_gcd(self):
+        import math
+        assert math.gcd(-4, -10) == 2
+        assert math.gcd(0, -10) == 10
+        assert math.gcd(0, 0) == 0
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to