mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
bpo-44698: Restore complex pow behaviour for small integral exponents (GH-27772) (GH-27796)
(cherry picked from commit 4b9a2dcf19
)
Co-authored-by: Mark Dickinson <mdickinson@enthought.com>
This commit is contained in:
parent
f6bd1ca166
commit
3f81e9628f
3 changed files with 37 additions and 21 deletions
|
@ -269,6 +269,34 @@ class ComplexTest(unittest.TestCase):
|
||||||
except OverflowError:
|
except OverflowError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def test_pow_with_small_integer_exponents(self):
|
||||||
|
# Check that small integer exponents are handled identically
|
||||||
|
# regardless of their type.
|
||||||
|
values = [
|
||||||
|
complex(5.0, 12.0),
|
||||||
|
complex(5.0e100, 12.0e100),
|
||||||
|
complex(-4.0, INF),
|
||||||
|
complex(INF, 0.0),
|
||||||
|
]
|
||||||
|
exponents = [-19, -5, -3, -2, -1, 0, 1, 2, 3, 5, 19]
|
||||||
|
for value in values:
|
||||||
|
for exponent in exponents:
|
||||||
|
with self.subTest(value=value, exponent=exponent):
|
||||||
|
try:
|
||||||
|
int_pow = value**exponent
|
||||||
|
except OverflowError:
|
||||||
|
int_pow = "overflow"
|
||||||
|
try:
|
||||||
|
float_pow = value**float(exponent)
|
||||||
|
except OverflowError:
|
||||||
|
float_pow = "overflow"
|
||||||
|
try:
|
||||||
|
complex_pow = value**complex(exponent)
|
||||||
|
except OverflowError:
|
||||||
|
complex_pow = "overflow"
|
||||||
|
self.assertEqual(str(float_pow), str(int_pow))
|
||||||
|
self.assertEqual(str(complex_pow), str(int_pow))
|
||||||
|
|
||||||
def test_boolcontext(self):
|
def test_boolcontext(self):
|
||||||
for i in range(100):
|
for i in range(100):
|
||||||
self.assertTrue(complex(random() + 1e-6, random() + 1e-6))
|
self.assertTrue(complex(random() + 1e-6, random() + 1e-6))
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Restore behaviour of complex exponentiation with integer-valued exponent of
|
||||||
|
type :class:`float` or :class:`complex`.
|
|
@ -172,14 +172,7 @@ c_powu(Py_complex x, long n)
|
||||||
static Py_complex
|
static Py_complex
|
||||||
c_powi(Py_complex x, long n)
|
c_powi(Py_complex x, long n)
|
||||||
{
|
{
|
||||||
Py_complex cn;
|
if (n > 0)
|
||||||
|
|
||||||
if (n > 100 || n < -100) {
|
|
||||||
cn.real = (double) n;
|
|
||||||
cn.imag = 0.;
|
|
||||||
return _Py_c_pow(x,cn);
|
|
||||||
}
|
|
||||||
else if (n > 0)
|
|
||||||
return c_powu(x,n);
|
return c_powu(x,n);
|
||||||
else
|
else
|
||||||
return _Py_c_quot(c_1, c_powu(x,-n));
|
return _Py_c_quot(c_1, c_powu(x,-n));
|
||||||
|
@ -523,19 +516,12 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
errno = 0;
|
errno = 0;
|
||||||
// Check if w is an integer value that fits inside a C long, so we can
|
// Check whether the exponent has a small integer value, and if so use
|
||||||
// use a faster algorithm. TO_COMPLEX(w, b), above, already handled the
|
// a faster and more accurate algorithm.
|
||||||
// conversion from larger longs, as well as other types.
|
if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= 100.0) {
|
||||||
if (PyLong_Check(w)) {
|
p = c_powi(a, (long)b.real);
|
||||||
int overflow = 0;
|
}
|
||||||
long int_exponent = PyLong_AsLongAndOverflow(w, &overflow);
|
else {
|
||||||
if (int_exponent == -1 && PyErr_Occurred())
|
|
||||||
return NULL;
|
|
||||||
if (overflow == 0)
|
|
||||||
p = c_powi(a, int_exponent);
|
|
||||||
else
|
|
||||||
p = _Py_c_pow(a, b);
|
|
||||||
} else {
|
|
||||||
p = _Py_c_pow(a, b);
|
p = _Py_c_pow(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue