Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)

from random import random
from math import isnan, copysign
from math import isnan, copysign, ulp
import operator

INF = float("inf")
Expand Down Expand Up @@ -446,6 +446,20 @@ def test_pow_with_small_integer_exponents(self):
self.assertEqual(str(float_pow), str(int_pow))
self.assertEqual(str(complex_pow), str(int_pow))

@support.requires_IEEE_754
def test_pow_small_negative_integer_exponents(self):
z = complex(float.fromhex('0x1.47e9c711723f5p+81'),
float.fromhex('0x1.38afd1168e49fp+85'))
expected = complex(float.fromhex('0x0.4000000000000p-1022'),
float.fromhex('0x0.3ffffffffffffp-1022'))
for exponent in (-12, -12.0, complex(-12.0, 0.0)):
with self.subTest(exponent=exponent):
result = z ** exponent
self.assertLessEqual(abs(result.real - expected.real),
4 * ulp(expected.real))
self.assertLessEqual(abs(result.imag - expected.imag),
4 * ulp(expected.imag))

def test_boolcontext(self):
for i in range(100):
self.assertTrue(complex(random() + 1e-6, random() + 1e-6))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Improve accuracy of :class:`complex` powers with small negative integer
exponents. Previously ``z**-n`` was computed as ``1/(z**n)``; the
intermediate ``z**n`` can overflow even when the result is representable,
in which case all precision was lost.
22 changes: 20 additions & 2 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,27 @@ c_powi(Py_complex x, long n)
{
if (n > 0)
return c_powu(x,n);
else
return _Py_c_quot(c_1, c_powu(x,-n));

Py_complex r = _Py_c_quot(c_1, c_powu(x, -n));
if (errno == EDOM
|| (isfinite(r.real) && isfinite(r.imag)
&& (r.real != 0.0 || r.imag != 0.0)))
return r;
Comment on lines +364 to +367

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any example, that trigger that case?


/* gh-156695: x**|n| left the exponent range although the result is
representable. Redo it with x scaled to exponent zero; both the
scaling and its undoing are exact. */
Comment on lines +369 to +371

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You recompute power and quotient again, unconditionally. I believe it will introduce a severe speed regression.

double m = fabs(x.real) > fabs(x.imag) ? fabs(x.real) : fabs(x.imag);
if (m == 0.0 || !isfinite(m))
return r;

int e;
frexp(m, &e);
Py_complex w = {ldexp(x.real, -e), ldexp(x.imag, -e)};
r = _Py_c_quot(c_1, c_powu(w, -n));
r.real = ldexp(r.real, (int)(e * n));
r.imag = ldexp(r.imag, (int)(e * n));
return r;
}

double
Expand Down
Loading