Skip to content

Commit 8f02c1e

Browse files
committed
improve tests
1 parent 11c2580 commit 8f02c1e

2 files changed

Lines changed: 15 additions & 23 deletions

File tree

Lib/test/test_complex.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
)
99

1010
from random import random
11-
from math import isnan, copysign, ulp
11+
from math import isnan, copysign
1212
import operator
1313

1414
INF = float("inf")
@@ -517,19 +517,13 @@ def test_pow_with_small_integer_exponents(self):
517517
self.assertNotEqual(r1.imag, 0.0)
518518
self.assertTrue(r2.real == 0.0 or r2.imag == 0.0)
519519

520-
@support.requires_IEEE_754
521520
def test_pow_small_negative_integer_exponents(self):
522-
z = complex(float.fromhex('0x1.47e9c711723f5p+81'),
523-
float.fromhex('0x1.38afd1168e49fp+85'))
524-
expected = complex(float.fromhex('0x0.4000000000000p-1022'),
525-
float.fromhex('0x0.3ffffffffffffp-1022'))
526-
for exponent in (-12, -12.0, complex(-12.0, 0.0)):
527-
with self.subTest(exponent=exponent):
528-
result = z ** exponent
529-
self.assertLessEqual(abs(result.real - expected.real),
530-
4 * ulp(expected.real))
531-
self.assertLessEqual(abs(result.imag - expected.imag),
532-
4 * ulp(expected.imag))
521+
for z, expected in [(complex(2**86, 0), complex(2**-1032, 0)),
522+
(complex(0, 2**86), complex(2**-1032, 0)),
523+
(complex(2**85, 2**85), complex(-2**-1026, 0))]:
524+
for exponent in (-12, -12.0, complex(-12)):
525+
with self.subTest(z=z, exponent=exponent):
526+
self.assertEqual(z**exponent, expected)
533527

534528
def test_boolcontext(self):
535529
for i in range(100):

Objects/complexobject.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,23 +368,21 @@ c_powu(Py_complex x, long n)
368368
static Py_complex
369369
c_powi(Py_complex x, long n)
370370
{
371-
if (n > 0)
372-
return c_powu(x,n);
373-
else if (n == 0)
374-
return (Py_complex){1., 0.};
375-
371+
if (n > 0) {
372+
return c_powu(x, n);
373+
}
374+
if (n == 0) {
375+
return (Py_complex){1., 0.};
376+
}
376377
double m = fabs(x.real) > fabs(x.imag) ? x.real : x.imag;
377-
378378
if (m && isfinite(m)) {
379379
int e;
380-
381380
frexp(m, &e);
382-
383381
if (-e*n > 800) {
384382
x = (Py_complex){ldexp(x.real, -e), ldexp(x.imag, -e)};
385383
x = _Py_rc_quot(1.0, c_powu(x, -n));
386-
x.real = ldexp(x.real, (int)(e * n));
387-
x.imag = ldexp(x.imag, (int)(e * n));
384+
x.real = ldexp(x.real, (int)(e*n));
385+
x.imag = ldexp(x.imag, (int)(e*n));
388386
return x;
389387
}
390388
}

0 commit comments

Comments
 (0)