Skip to content

Commit af7b478

Browse files
committed
pythongh-117999: fixed invalid small integer powers of real±0j
1 parent 64cd6fc commit af7b478

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Lib/test/test_complex.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,13 @@ def test_pow_with_small_integer_exponents(self):
330330
self.assertEqual(str(float_pow), str(int_pow))
331331
self.assertEqual(str(complex_pow), str(int_pow))
332332

333+
self.assertFloatsAreIdentical(pow(complex(1,+0.0), 0).imag, +0.0)
334+
self.assertFloatsAreIdentical(pow(complex(1,-0.0), 0).imag, +0.0)
335+
self.assertFloatsAreIdentical(pow(complex(1,+0.0), 2).imag, +0.0)
336+
self.assertFloatsAreIdentical(pow(complex(1,-0.0), 2).imag, -0.0)
337+
self.assertFloatsAreIdentical(pow(complex(1,+0.0), -3).imag, -0.0)
338+
self.assertFloatsAreIdentical(pow(complex(1,-0.0), -3).imag, +0.0)
339+
333340
def test_boolcontext(self):
334341
for i in range(100):
335342
self.assertTrue(complex(random() + 1e-6, random() + 1e-6))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed invalid small integer powers for complex numbers with ``±0.0`` in
2+
imaginary component. Patch by Sergey B Kirpichev.

Objects/complexobject.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ c_powu(Py_complex x, long n)
162162
Py_complex r, p;
163163
long mask = 1;
164164
r = c_1;
165+
if (n)
166+
r.imag = copysign(0.0, x.imag);
165167
p = x;
166168
while (mask > 0 && n >= mask) {
167169
if (n & mask)
@@ -177,9 +179,10 @@ c_powi(Py_complex x, long n)
177179
{
178180
if (n > 0)
179181
return c_powu(x,n);
180-
else
182+
else {
183+
c_1.imag = -copysign(0.0, x.imag);
181184
return _Py_c_quot(c_1, c_powu(x,-n));
182-
185+
}
183186
}
184187

185188
double

0 commit comments

Comments
 (0)