mirror of
https://github.com/python/cpython.git
synced 2025-11-30 14:59:12 +00:00
Issue #27539: Fix unnormalised Fraction.__pow__ result for negative exponent and base. Thanks Vedran Čačić.
This commit is contained in:
parent
6afe85827c
commit
844796530a
4 changed files with 22 additions and 1 deletions
|
|
@ -484,10 +484,14 @@ class Fraction(numbers.Rational):
|
||||||
return Fraction(a._numerator ** power,
|
return Fraction(a._numerator ** power,
|
||||||
a._denominator ** power,
|
a._denominator ** power,
|
||||||
_normalize=False)
|
_normalize=False)
|
||||||
else:
|
elif a._numerator >= 0:
|
||||||
return Fraction(a._denominator ** -power,
|
return Fraction(a._denominator ** -power,
|
||||||
a._numerator ** -power,
|
a._numerator ** -power,
|
||||||
_normalize=False)
|
_normalize=False)
|
||||||
|
else:
|
||||||
|
return Fraction((-a._denominator) ** -power,
|
||||||
|
(-a._numerator) ** -power,
|
||||||
|
_normalize=False)
|
||||||
else:
|
else:
|
||||||
# A fractional power will generally produce an
|
# A fractional power will generally produce an
|
||||||
# irrational number.
|
# irrational number.
|
||||||
|
|
|
||||||
|
|
@ -356,6 +356,19 @@ class FractionTest(unittest.TestCase):
|
||||||
z = pow(F(-1), F(1, 2))
|
z = pow(F(-1), F(1, 2))
|
||||||
self.assertAlmostEqual(z.real, 0)
|
self.assertAlmostEqual(z.real, 0)
|
||||||
self.assertEqual(z.imag, 1)
|
self.assertEqual(z.imag, 1)
|
||||||
|
# Regression test for #27539.
|
||||||
|
p = F(-1, 2) ** 0
|
||||||
|
self.assertEqual(p, F(1, 1))
|
||||||
|
self.assertEqual(p.numerator, 1)
|
||||||
|
self.assertEqual(p.denominator, 1)
|
||||||
|
p = F(-1, 2) ** -1
|
||||||
|
self.assertEqual(p, F(-2, 1))
|
||||||
|
self.assertEqual(p.numerator, -2)
|
||||||
|
self.assertEqual(p.denominator, 1)
|
||||||
|
p = F(-1, 2) ** -2
|
||||||
|
self.assertEqual(p, F(4, 1))
|
||||||
|
self.assertEqual(p.numerator, 4)
|
||||||
|
self.assertEqual(p.denominator, 1)
|
||||||
|
|
||||||
def testMixedArithmetic(self):
|
def testMixedArithmetic(self):
|
||||||
self.assertTypedEquals(F(11, 10), F(1, 10) + 1)
|
self.assertTypedEquals(F(11, 10), F(1, 10) + 1)
|
||||||
|
|
|
||||||
|
|
@ -217,6 +217,7 @@ Katherine Busch
|
||||||
Ralph Butler
|
Ralph Butler
|
||||||
Laurent De Buyst
|
Laurent De Buyst
|
||||||
Zach Byrne
|
Zach Byrne
|
||||||
|
Vedran Čačić
|
||||||
Nicolas Cadou
|
Nicolas Cadou
|
||||||
Jp Calderone
|
Jp Calderone
|
||||||
Arnaud Calmettes
|
Arnaud Calmettes
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #27539: Fix unnormalised ``Fraction.__pow__`` result in the case
|
||||||
|
of negative exponent and negative base.
|
||||||
|
|
||||||
- Issue #21718: cursor.description is now available for queries using CTEs.
|
- Issue #21718: cursor.description is now available for queries using CTEs.
|
||||||
|
|
||||||
- Issue #2466: posixpath.ismount now correctly recognizes mount points which
|
- Issue #2466: posixpath.ismount now correctly recognizes mount points which
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue