Skip to content

Commit ee0a74f

Browse files
committed
address review: correct zero division case and add inf**1j test
1 parent 256a454 commit ee0a74f

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

Include/internal/pycore_complexobject.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ _Py_c_isfinite(Py_complex a)
4545
return isfinite(a.real) && isfinite(a.imag);
4646
}
4747

48+
static inline bool
49+
_Py_c_iszero(Py_complex a)
50+
{
51+
return a.real == 0.0 && a.imag == 0.0;
52+
}
53+
4854
#ifdef __cplusplus
4955
}
5056
#endif

Lib/test/test_complex.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ def test_pow(self):
370370
r = pow(complex(INF), 2.25) # generic algorithm
371371
self.assertEqual(r.real, INF)
372372
self.assertTrue(isnan(r.imag))
373+
r = pow(complex(INF), 1j)
374+
self.assertTrue(isnan(r.real))
375+
self.assertTrue(isnan(r.imag))
373376

374377
a = 3.33+4.43j
375378
self.assertEqual(a ** 0j, 1)

Objects/complexobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ _Py_c_quot(Py_complex a, Py_complex b)
301301
{
302302
Py_complex r = c_quot(a, b);
303303

304-
if (_Py_c_isnan(r) && b.real == 0 && b.imag == 0) {
304+
if (_Py_c_isnan(r) && _Py_c_iszero(b)) {
305305
errno = EDOM;
306306
r.real = r.imag = 0.0; /* and set r as documented */
307307
}
@@ -314,11 +314,11 @@ c_pow(Py_complex a, Py_complex b)
314314
Py_complex r;
315315
double vabs,len,at,phase;
316316

317-
if (b.real == 0. && b.imag == 0.) {
317+
if (_Py_c_iszero(b)) {
318318
r.real = 1.;
319319
r.imag = 0.;
320320
}
321-
else if (a.real == 0. && a.imag == 0.) {
321+
else if (_Py_c_iszero(a)) {
322322
if (b.imag != 0. || b.real < 0.) {
323323
r.real = NAN;
324324
r.imag = NAN;
@@ -780,7 +780,7 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
780780
else {
781781
p = c_pow(a, b);
782782
}
783-
if (_Py_c_isnan(p) && a.real == 0 && a.imag == 0) {
783+
if (_Py_c_isnan(p) && _Py_c_iszero(a) && (b.imag || b.real < 0)) {
784784
PyErr_SetString(PyExc_ZeroDivisionError,
785785
"zero to a negative or complex power");
786786
return NULL;

0 commit comments

Comments
 (0)