diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2016-08-22 09:50:53 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2016-08-22 09:50:53 (GMT) |
commit | 844796530a21f2a8689f2b9e01035d4a64a95275 (patch) | |
tree | 1866b84a3126cf68b121447bdcd5cb118c4f7896 | |
parent | 6afe85827c209b9d1e76a65ffdb7420b5f46ad3d (diff) | |
download | cpython-844796530a21f2a8689f2b9e01035d4a64a95275.zip cpython-844796530a21f2a8689f2b9e01035d4a64a95275.tar.gz cpython-844796530a21f2a8689f2b9e01035d4a64a95275.tar.bz2 |
Issue #27539: Fix unnormalised Fraction.__pow__ result for negative exponent and base. Thanks Vedran Čačić.
-rw-r--r-- | Lib/fractions.py | 6 | ||||
-rw-r--r-- | Lib/test/test_fractions.py | 13 | ||||
-rw-r--r-- | Misc/ACKS | 1 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
4 files changed, 22 insertions, 1 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py index 60b0728..9aabab3 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -484,10 +484,14 @@ class Fraction(numbers.Rational): return Fraction(a._numerator ** power, a._denominator ** power, _normalize=False) - else: + elif a._numerator >= 0: return Fraction(a._denominator ** -power, a._numerator ** -power, _normalize=False) + else: + return Fraction((-a._denominator) ** -power, + (-a._numerator) ** -power, + _normalize=False) else: # A fractional power will generally produce an # irrational number. diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 1699852..9df4a54 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -356,6 +356,19 @@ class FractionTest(unittest.TestCase): z = pow(F(-1), F(1, 2)) self.assertAlmostEqual(z.real, 0) self.assertEqual(z.imag, 1) + # Regression test for #27539. + p = F(-1, 2) ** 0 + self.assertEqual(p, F(1, 1)) + self.assertEqual(p.numerator, 1) + self.assertEqual(p.denominator, 1) + p = F(-1, 2) ** -1 + self.assertEqual(p, F(-2, 1)) + self.assertEqual(p.numerator, -2) + self.assertEqual(p.denominator, 1) + p = F(-1, 2) ** -2 + self.assertEqual(p, F(4, 1)) + self.assertEqual(p.numerator, 4) + self.assertEqual(p.denominator, 1) def testMixedArithmetic(self): self.assertTypedEquals(F(11, 10), F(1, 10) + 1) @@ -217,6 +217,7 @@ Katherine Busch Ralph Butler Laurent De Buyst Zach Byrne +Vedran Čačić Nicolas Cadou Jp Calderone Arnaud Calmettes @@ -46,6 +46,9 @@ Core and Builtins Library ------- +- Issue #27539: Fix unnormalised ``Fraction.__pow__`` result in the case + of negative exponent and negative base. + - Issue #21718: cursor.description is now available for queries using CTEs. - Issue #2466: posixpath.ismount now correctly recognizes mount points which |