[3.12] gh-119189: Fix the power operator for Fraction (GH-119242) (GH-119835)

When using the ** operator or pow() with Fraction as the base
and an exponent that is not rational, a float, or a complex, the
fraction is no longer converted to a float.
(cherry picked from commit b9965ef282)

Co-authored-by: Joshua Herman <30265+zitterbewegung@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2024-07-16 09:44:05 +02:00 committed by GitHub
parent b455a5a55c
commit d4f57ccc3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 12 additions and 6 deletions

View file

@ -825,8 +825,10 @@ class Fraction(numbers.Rational):
# A fractional power will generally produce an # A fractional power will generally produce an
# irrational number. # irrational number.
return float(a) ** float(b) return float(a) ** float(b)
else: elif isinstance(b, (float, complex)):
return float(a) ** b return float(a) ** b
else:
return NotImplemented
def __rpow__(b, a): def __rpow__(b, a):
"""a ** b""" """a ** b"""

View file

@ -922,21 +922,21 @@ class FractionTest(unittest.TestCase):
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, 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.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.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(3, 2) ** Rect(2, 0), Polar(F(9,4), 0.0))
self.assertTypedEquals(F(1, 1) ** Rect(2, 3), Polar(1.0, 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(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(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, 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(64, 6))
self.assertTypedEquals(Polar(4, 2) ** F(-3, 1), Polar(0.015625, -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.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(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')) self.assertEqual(Symbolic('X') ** F(3, 2), Symbolic('X ** 1.5'))
def testMixingWithDecimal(self): def testMixingWithDecimal(self):

View file

@ -744,6 +744,7 @@ Kasun Herath
Chris Herborth Chris Herborth
Ivan Herman Ivan Herman
Jürgen Hermann Jürgen Hermann
Joshua Jay Herman
Gary Herron Gary Herron
Ernie Hershey Ernie Hershey
Thomas Herve Thomas Herve

View file

@ -0,0 +1,3 @@
When using the ``**`` operator or :func:`pow` with :class:`~fractions.Fraction`
as the base and an exponent that is not rational, a float, or a complex, the
fraction is no longer converted to a float.