Skip to content

Commit d4f57cc

Browse files
[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 b9965ef) Co-authored-by: Joshua Herman <[email protected]>
1 parent b455a5a commit d4f57cc

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

Lib/fractions.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,10 @@ def __pow__(a, b):
825825
# A fractional power will generally produce an
826826
# irrational number.
827827
return float(a) ** float(b)
828-
else:
828+
elif isinstance(b, (float, complex)):
829829
return float(a) ** b
830+
else:
831+
return NotImplemented
830832

831833
def __rpow__(b, a):
832834
"""a ** b"""

Lib/test/test_fractions.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -922,21 +922,21 @@ def testMixedPower(self):
922922
self.assertTypedEquals(Root(4) ** F(2, 1), Root(4, F(1)))
923923
self.assertTypedEquals(Root(4) ** F(-2, 1), Root(4, -F(1)))
924924
self.assertTypedEquals(Root(4) ** F(-2, 3), Root(4, -3.0))
925-
self.assertEqual(F(3, 2) ** SymbolicReal('X'), SymbolicReal('1.5 ** X'))
925+
self.assertEqual(F(3, 2) ** SymbolicReal('X'), SymbolicReal('3/2 ** X'))
926926
self.assertEqual(SymbolicReal('X') ** F(3, 2), SymbolicReal('X ** 1.5'))
927927

928-
self.assertTypedEquals(F(3, 2) ** Rect(2, 0), Polar(2.25, 0.0))
929-
self.assertTypedEquals(F(1, 1) ** Rect(2, 3), Polar(1.0, 0.0))
928+
self.assertTypedEquals(F(3, 2) ** Rect(2, 0), Polar(F(9,4), 0.0))
929+
self.assertTypedEquals(F(1, 1) ** Rect(2, 3), Polar(F(1), 0.0))
930930
self.assertTypedEquals(F(3, 2) ** RectComplex(2, 0), Polar(2.25, 0.0))
931931
self.assertTypedEquals(F(1, 1) ** RectComplex(2, 3), Polar(1.0, 0.0))
932932
self.assertTypedEquals(Polar(4, 2) ** F(3, 2), Polar(8.0, 3.0))
933933
self.assertTypedEquals(Polar(4, 2) ** F(3, 1), Polar(64, 6))
934934
self.assertTypedEquals(Polar(4, 2) ** F(-3, 1), Polar(0.015625, -6))
935935
self.assertTypedEquals(Polar(4, 2) ** F(-3, 2), Polar(0.125, -3.0))
936-
self.assertEqual(F(3, 2) ** SymbolicComplex('X'), SymbolicComplex('1.5 ** X'))
936+
self.assertEqual(F(3, 2) ** SymbolicComplex('X'), SymbolicComplex('3/2 ** X'))
937937
self.assertEqual(SymbolicComplex('X') ** F(3, 2), SymbolicComplex('X ** 1.5'))
938938

939-
self.assertEqual(F(3, 2) ** Symbolic('X'), Symbolic('1.5 ** X'))
939+
self.assertEqual(F(3, 2) ** Symbolic('X'), Symbolic('3/2 ** X'))
940940
self.assertEqual(Symbolic('X') ** F(3, 2), Symbolic('X ** 1.5'))
941941

942942
def testMixingWithDecimal(self):

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ Kasun Herath
744744
Chris Herborth
745745
Ivan Herman
746746
Jürgen Hermann
747+
Joshua Jay Herman
747748
Gary Herron
748749
Ernie Hershey
749750
Thomas Herve
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When using the ``**`` operator or :func:`pow` with :class:`~fractions.Fraction`
2+
as the base and an exponent that is not rational, a float, or a complex, the
3+
fraction is no longer converted to a float.

0 commit comments

Comments
 (0)