Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Address review: subTest and asserts
  • Loading branch information
skirpichev committed Aug 19, 2024
commit 807bcc6184b74a0ac179d06cf7f3b703bc6240c0
63 changes: 32 additions & 31 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,37 +362,38 @@ def test_pow_with_small_integer_exponents(self):
exponents = [0, 1, 2, 3, 4, 5, 6, 19]
for z in values:
for e in exponents:
try:
r_pow = z**e
except OverflowError:
continue
r_pro = reduce(lambda x, y: x*y, [z]*e) if e else 1+0j
if str(r_pow) == str(r_pro):
continue

self.assertNotIn(z.real, {0.0, -0.0, INF, -INF, NAN})
self.assertNotIn(z.imag, {0.0, -0.0, INF, -INF, NAN})

# We might fail here, because associativity of multiplication
# is broken already for floats.
# Consider z = 1-1j. Then z*z*z*z = ((z*z)*z)*z = -4+0j,
# while in the algorithm for pow() a diffenent grouping
# of operations is used: z**4 = (z*z)*(z*z) = -4-0j.
#
# Fallback to the generic complex power algorithm.
r_pro, r_pro_errno = _testcapi._py_c_pow(z, e)
self.assertEqual(r_pro_errno, 0)
self.assertClose(r_pow, r_pro)
if isnan(r_pow.real):
self.assertTrue(isnan(r_pro.real))
else:
self.assertEqual(copysign(1, r_pow.real),
copysign(1, r_pro.real))
if isnan(r_pow.imag):
self.assertTrue(isnan(r_pro.imag))
else:
self.assertEqual(copysign(1, r_pow.imag),
copysign(1, r_pro.imag))
with self.subTest(value=z, exponent=e):
try:
r_pow = z**e
except OverflowError:
continue
r_pro = reduce(lambda x, y: x*y, [z]*e) if e else 1+0j
if str(r_pow) == str(r_pro):
continue

self.assertNotIn(z.real, {0.0, -0.0, INF, -INF, NAN})
self.assertNotIn(z.imag, {0.0, -0.0, INF, -INF, NAN})

# We might fail here, because associativity of multiplication
# is broken already for floats.
# Consider z = 1-1j. Then z*z*z*z = ((z*z)*z)*z = -4+0j,
# while in the algorithm for pow() a diffenent grouping
# of operations is used: z**4 = (z*z)*(z*z) = -4-0j.
#
# Fallback to the generic complex power algorithm.
r_pro, r_pro_errno = _testcapi._py_c_pow(z, e)
self.assertEqual(r_pro_errno, 0)
self.assertClose(r_pow, r_pro)
if isnan(r_pow.real):
self.assertTrue(isnan(r_pro.real))
else:
self.assertEqual(copysign(1, r_pow.real),
copysign(1, r_pro.real))
if isnan(r_pow.imag):
self.assertTrue(isnan(r_pro.imag))
else:
self.assertEqual(copysign(1, r_pow.imag),
copysign(1, r_pro.imag))

def test_boolcontext(self):
for i in range(100):
Expand Down
3 changes: 2 additions & 1 deletion Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ c_powu(Py_complex x, long n)
{
Py_complex p = x, r = n-- ? p : c_1;
long mask = 1;
assert(-1 <= n && n < INT_EXP_CUTOFF);
assert(-1 <= n);
assert(n < INT_EXP_CUTOFF);
while (n >= mask) {
assert(mask>0);
if (n & mask)
Expand Down
Loading