Skip to content

Commit

Permalink
pythongh-117999: fixed invalid small integer powers of real±0j
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Apr 18, 2024
1 parent 64cd6fc commit af7b478
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ def test_pow_with_small_integer_exponents(self):
self.assertEqual(str(float_pow), str(int_pow))
self.assertEqual(str(complex_pow), str(int_pow))

self.assertFloatsAreIdentical(pow(complex(1,+0.0), 0).imag, +0.0)
self.assertFloatsAreIdentical(pow(complex(1,-0.0), 0).imag, +0.0)
self.assertFloatsAreIdentical(pow(complex(1,+0.0), 2).imag, +0.0)
self.assertFloatsAreIdentical(pow(complex(1,-0.0), 2).imag, -0.0)
self.assertFloatsAreIdentical(pow(complex(1,+0.0), -3).imag, -0.0)
self.assertFloatsAreIdentical(pow(complex(1,-0.0), -3).imag, +0.0)

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,2 @@
Fixed invalid small integer powers for complex numbers with ``±0.0`` in
imaginary component. Patch by Sergey B Kirpichev.
7 changes: 5 additions & 2 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ c_powu(Py_complex x, long n)
Py_complex r, p;
long mask = 1;
r = c_1;
if (n)
r.imag = copysign(0.0, x.imag);
p = x;
while (mask > 0 && n >= mask) {
if (n & mask)
Expand All @@ -177,9 +179,10 @@ c_powi(Py_complex x, long n)
{
if (n > 0)
return c_powu(x,n);
else
else {
c_1.imag = -copysign(0.0, x.imag);
return _Py_c_quot(c_1, c_powu(x,-n));

}
}

double
Expand Down

0 comments on commit af7b478

Please sign in to comment.