Skip to content

Commit

Permalink
Fix the power operator for non float/complex values.
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed Nov 28, 2024
1 parent 123fa7c commit 509036c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/quicktions.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 509036c

Please sign in to comment.