From 509036c6786bc3f06788f0cd98cd33aad9f2cd62 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Thu, 28 Nov 2024 12:21:00 +0100 Subject: [PATCH] Fix the power operator for non float/complex values. See https://github.com/python/cpython/pull/119242 --- src/quicktions.pyx | 4 +++- src/test_fractions.py | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/quicktions.pyx b/src/quicktions.pyx index be04631..bfdf8ba 100644 --- a/src/quicktions.pyx +++ b/src/quicktions.pyx @@ -925,8 +925,10 @@ cdef class Fraction: return _pow(a.numerator, a.denominator, b, 1) elif isinstance(b, (Fraction, Rational)): return _pow(a.numerator, a.denominator, b.numerator, b.denominator) - else: + elif isinstance(b, (float, complex)): return (a.numerator / a.denominator) ** b + else: + return NotImplemented def __rpow__(b, a, x): """a ** b diff --git a/src/test_fractions.py b/src/test_fractions.py index 9503460..e2d91f9 100644 --- a/src/test_fractions.py +++ b/src/test_fractions.py @@ -1129,21 +1129,21 @@ def testMixedPower(self): self.assertTypedEquals(Root(4) ** F(2, 1), Root(4, F(1))) self.assertTypedEquals(Root(4) ** F(-2, 1), Root(4, -F(1))) self.assertTypedEquals(Root(4) ** F(-2, 3), Root(4, -3.0)) - self.assertEqual(F(3, 2) ** SymbolicReal('X'), SymbolicReal('1.5 ** X')) + self.assertEqual(F(3, 2) ** SymbolicReal('X'), SymbolicReal('3/2 ** X')) self.assertEqual(SymbolicReal('X') ** F(3, 2), SymbolicReal('X ** 1.5')) - self.assertTypedEquals(F(3, 2) ** Rect(2, 0), Polar(2.25, 0.0)) - self.assertTypedEquals(F(1, 1) ** Rect(2, 3), Polar(1.0, 0.0)) + self.assertTypedEquals(F(3, 2) ** Rect(2, 0), Polar(F(9,4), 0.0)) + self.assertTypedEquals(F(1, 1) ** Rect(2, 3), Polar(F(1), 0.0)) self.assertTypedEquals(F(3, 2) ** RectComplex(2, 0), Polar(2.25, 0.0)) self.assertTypedEquals(F(1, 1) ** RectComplex(2, 3), Polar(1.0, 0.0)) self.assertTypedEquals(Polar(4, 2) ** F(3, 2), Polar(8.0, 3.0)) self.assertTypedEquals(Polar(4, 2) ** F(3, 1), Polar(64, 6)) self.assertTypedEquals(Polar(4, 2) ** F(-3, 1), Polar(0.015625, -6)) self.assertTypedEquals(Polar(4, 2) ** F(-3, 2), Polar(0.125, -3.0)) - self.assertEqual(F(3, 2) ** SymbolicComplex('X'), SymbolicComplex('1.5 ** X')) + self.assertEqual(F(3, 2) ** SymbolicComplex('X'), SymbolicComplex('3/2 ** X')) self.assertEqual(SymbolicComplex('X') ** F(3, 2), SymbolicComplex('X ** 1.5')) - self.assertEqual(F(3, 2) ** Symbolic('X'), Symbolic('1.5 ** X')) + self.assertEqual(F(3, 2) ** Symbolic('X'), Symbolic('3/2 ** X')) self.assertEqual(Symbolic('X') ** F(3, 2), Symbolic('X ** 1.5')) def testMixingWithDecimal(self):