summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2019-06-02 09:24:06 (GMT)
committerGitHub <noreply@github.com>2019-06-02 09:24:06 (GMT)
commitc52996785a45d4693857ea219e040777a14584f8 (patch)
treeee1d54ea597b45ef0d57407f8affaffe57f93be6 /Lib
parent5ae299ac78abb628803ab7dee0997364547f5cc8 (diff)
downloadcpython-c52996785a45d4693857ea219e040777a14584f8.zip
cpython-c52996785a45d4693857ea219e040777a14584f8.tar.gz
cpython-c52996785a45d4693857ea219e040777a14584f8.tar.bz2
bpo-36027: Extend three-argument pow to negative second argument (GH-13266)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_builtin.py3
-rw-r--r--Lib/test/test_pow.py26
2 files changed, 28 insertions, 1 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index e32fb75..b536cec 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1195,7 +1195,8 @@ class BuiltinTest(unittest.TestCase):
self.assertAlmostEqual(pow(-1, 0.5), 1j)
self.assertAlmostEqual(pow(-1, 1/3), 0.5 + 0.8660254037844386j)
- self.assertRaises(ValueError, pow, -1, -2, 3)
+ # See test_pow for additional tests for three-argument pow.
+ self.assertEqual(pow(-1, -2, 3), 1)
self.assertRaises(ValueError, pow, 1, 2, 0)
self.assertRaises(TypeError, pow)
diff --git a/Lib/test/test_pow.py b/Lib/test/test_pow.py
index cac1ae5..660ff80 100644
--- a/Lib/test/test_pow.py
+++ b/Lib/test/test_pow.py
@@ -1,3 +1,4 @@
+import math
import unittest
class PowTest(unittest.TestCase):
@@ -119,5 +120,30 @@ class PowTest(unittest.TestCase):
eq(pow(a, -fiveto), expected)
eq(expected, 1.0) # else we didn't push fiveto to evenness
+ def test_negative_exponent(self):
+ for a in range(-50, 50):
+ for m in range(-50, 50):
+ with self.subTest(a=a, m=m):
+ if m != 0 and math.gcd(a, m) == 1:
+ # Exponent -1 should give an inverse, with the
+ # same sign as m.
+ inv = pow(a, -1, m)
+ self.assertEqual(inv, inv % m)
+ self.assertEqual((inv * a - 1) % m, 0)
+
+ # Larger exponents
+ self.assertEqual(pow(a, -2, m), pow(inv, 2, m))
+ self.assertEqual(pow(a, -3, m), pow(inv, 3, m))
+ self.assertEqual(pow(a, -1001, m), pow(inv, 1001, m))
+
+ else:
+ with self.assertRaises(ValueError):
+ pow(a, -1, m)
+ with self.assertRaises(ValueError):
+ pow(a, -2, m)
+ with self.assertRaises(ValueError):
+ pow(a, -1001, m)
+
+
if __name__ == "__main__":
unittest.main()