From 844796530a21f2a8689f2b9e01035d4a64a95275 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Mon, 22 Aug 2016 10:50:53 +0100 Subject: [PATCH] =?utf8?q?Issue=20#27539:=20Fix=20unnormalised=20Fraction.?= =?utf8?q?=5F=5Fpow=5F=5F=20result=20for=20negative=20exponent=20and=20bas?= =?utf8?q?e.=20Thanks=20Vedran=20=C4=8Ca=C4=8Di=C4=87.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Lib/fractions.py | 6 +++++- Lib/test/test_fractions.py | 13 +++++++++++++ Misc/ACKS | 1 + Misc/NEWS | 3 +++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Lib/fractions.py b/Lib/fractions.py index 60b072880703..9aabab3e3ba9 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 16998522160b..9df4a54fcbde 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) diff --git a/Misc/ACKS b/Misc/ACKS index 51c8d101d02d..1c9363aab671 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -217,6 +217,7 @@ Katherine Busch Ralph Butler Laurent De Buyst Zach Byrne +Vedran Čačić Nicolas Cadou Jp Calderone Arnaud Calmettes diff --git a/Misc/NEWS b/Misc/NEWS index 54cae1da9fb5..7530624751b6 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -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 -- 2.47.3