Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bab22be

Browse files
committed
SF bug 533198: Complex power underflow raises exception.
Konrad was too kind. Not only did it raise an exception, the specific exception it raised made no sense. These are old bugs in complex_pow() and friends: 1. Raising 0 to a negative power isn't a range error, it's a domain error, so changed c_pow() to set errno to EDOM in that case instead of ERANGE. 2. Changed complex_pow() to: A. Used the Py_ADJUST_ERANGE2 macro to try to clear errno of a spurious ERANGE error due to underflow in the libm pow() called by c_pow(). B. Produced different exceptions depending on the errno value: i) For errno==EDOM, raise ZeroDivisionError instead of ValueError. This is for consistency with the non-complex cases 0.0**-2 and 0**-2 and 0L**-2. ii) For errno==ERANGE, raise OverflowError. Bugfix candidate.
1 parent 366a1df commit bab22be

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

Objects/complexobject.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ c_pow(Py_complex a, Py_complex b)
131131
}
132132
else if (a.real == 0. && a.imag == 0.) {
133133
if (b.imag != 0. || b.real < 0.)
134-
errno = ERANGE;
134+
errno = EDOM;
135135
r.real = 0.;
136136
r.imag = 0.;
137137
}
@@ -456,11 +456,17 @@ complex_pow(PyComplexObject *v, PyObject *w, PyComplexObject *z)
456456
p = c_pow(v->cval,exponent);
457457

458458
PyFPE_END_PROTECT(p)
459-
if (errno == ERANGE) {
460-
PyErr_SetString(PyExc_ValueError,
459+
Py_ADJUST_ERANGE2(p.real, p.imag);
460+
if (errno == EDOM) {
461+
PyErr_SetString(PyExc_ZeroDivisionError,
461462
"0.0 to a negative or complex power");
462463
return NULL;
463464
}
465+
else if (errno == ERANGE) {
466+
PyErr_SetString(PyExc_OverflowError,
467+
"complex exponentiaion");
468+
return NULL;
469+
}
464470
return PyComplex_FromCComplex(p);
465471
}
466472

0 commit comments

Comments
 (0)