8000 gh-119189: Fix the power operator for Fraction (GH-119242) · python/cpython@b9965ef · GitHub
[go: up one dir, main page]

Skip to content

Commit b9965ef

Browse files
gh-119189: Fix the power operator for Fraction (GH-119242)
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.
1 parent dae0375 commit b9965ef

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

Lib/fractions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,10 @@ def __pow__(a, b, modulo=None):
877877
# A fractional power will generally produce an
878878
# irrational number.
879879
return float(a) ** float(b)
880-
else:
880+
elif isinstance(b, (float, complex)):
881881
return float(a) ** b
882+
else:
883+
return NotImplemented
882884

883885
def __rpow__(b, a):
884886
"""a ** b"""

Lib/test/test_fractions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,21 +925,21 @@ def testMixedPower(self):
925925
self.assertTypedEquals(Root(4) ** F(2, 1), Root(4, F(1)))
926926
self.assertTypedEquals(Root(4) ** F(-2, 1), Root(4, -F(1)))
927927
self.assertTypedEquals(Root(4) ** F(-2, 3), Root(4, -3.0))
928-
self.assertEqual(F(3, 2) ** SymbolicReal('X'), SymbolicReal('1.5 ** X'))
928+
self.assertEqual(F(3, 2) ** SymbolicReal('X'), SymbolicReal('3/2 ** X'))
929929
self.assertEqual(SymbolicReal('X') ** F(3, 2), SymbolicReal('X ** 1.5'))
930930

931-
self.assertTypedEquals(F(3, 2) ** Rect(2, 0), Polar(2.25, 0.0))
932-
self.assertTypedEquals(F(1, 1) ** Rect(2, 3), Polar(1.0, 0.0))
931+
self.assertTypedEquals(F(3, 2) ** Rect(2, 0), Polar(F(9,4), 0.0))
932+
self.assertTypedEquals(F(1, 1) ** Rect(2, 3), Polar(F(1), 0.0))
933933
self.assertTypedEquals(F(3, 2) ** RectComplex(2, 0), Polar(2.25, 0.0))
934934
self.assertTypedEquals(F(1, 1) ** RectComplex(2, 3), Polar(1.0, 0.0))
935935
self.assertTypedEquals(Polar(4, 2) ** F(3, 2), Polar(8.0, 3.0))
936936
self.assertTypedEquals(Polar(4, 2) ** F(3, 1), Polar(64, 6))
937937
self.assertTypedEquals(Polar(4, 2) ** F(-3, 1), Polar(0.015625, -6))
938938
self.assertTypedEquals(Polar(4, 2) ** F(-3, 2), Polar(0.125, -3.0))
939-
self.assertEqual(F(3, 2) ** SymbolicComplex('X'), SymbolicComplex('1.5 ** X'))
939+
self.assertEqual(F(3, 2) ** SymbolicComplex('X'), SymbolicComplex('3/2 ** X'))
940940
self.assertEqual(SymbolicComplex('X') ** F(3, 2), SymbolicComplex('X ** 1.5'))
941941

942-
self.assertEqual(F(3, 2) ** Symbolic('X'), Symbolic('1.5 ** X'))
942+
self.assertEqual(F(3, 2) ** Symbolic('X'), Symbolic('3/2 ** X'))
943943
self.assertEqual(Symbolic('X') ** F(3, 2), Symbolic('X ** 1.5'))
944944

945945
def testMixingWithDecimal(self):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ Kasun Herath
751751
Chris Herborth
752752
Ivan Herman
753753
Jürgen Hermann
754+
Joshua Jay Herman
754755
Gary Herron
755756
Ernie Hershey
756757
Thomas Herve
6C80
Lines changed: 3 additions & 0 deletions
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)
0