Skip to content

Commit 7e68e91

Browse files
committed
from 119771: adjust errno on overflows in _Py_c_pow()
Before we did this in complex_pow() and behaviour of the public C API function ``_Py_c_pow()`` was different from the pure-python pow().
1 parent 142b753 commit 7e68e91

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

Doc/c-api/complex.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ pointers. This is consistent throughout the API.
7979
If *num* is null and *exp* is not a positive real number,
8080
this method returns zero and sets :c:data:`errno` to :c:macro:`!EDOM`.
8181
82+
Set :c:data:`errno` to :c:macro:`!ERANGE` on overflows.
83+
8284
8385
Complex Numbers as Python Objects
8486
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Lib/test/test_capi/test_complex.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ def test_py_c_pow(self):
229229

230230
self.assertEqual(_py_c_pow(0j, -1)[1], errno.EDOM)
231231
self.assertEqual(_py_c_pow(0j, 1j)[1], errno.EDOM)
232-
self.assertEqual(_py_c_pow(*[DBL_MAX+1j]*2)[0], complex(*[INF]*2))
232+
self.assertEqual(_py_c_pow(*[DBL_MAX+1j]*2),
233+
(complex(*[INF]*2), errno.ERANGE))
234+
self.assertEqual(_py_c_pow(DBL_MAX+1j, 2),
235+
(complex(*[INF]*2), errno.ERANGE))
233236

234237
def test_py_c_abs(self):
235238
# Test _Py_c_abs()

Objects/complexobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ _Py_c_pow(Py_complex a, Py_complex b)
278278
}
279279
r.real = len*cos(phase);
280280
r.imag = len*sin(phase);
281+
282+
_Py_ADJUST_ERANGE2(r.real, r.imag);
281283
}
282284
return r;
283285
}
@@ -773,12 +775,12 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
773775
// a faster and more accurate algorithm.
774776
if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= 100.0) {
775777
p = c_powi(a, (long)b.real);
778+
_Py_ADJUST_ERANGE2(p.real, p.imag);
776779
}
777780
else {
778781
p = _Py_c_pow(a, b);
779782
}
780783

781-
_Py_ADJUST_ERANGE2(p.real, p.imag);
782784
if (errno == EDOM) {
783785
PyErr_SetString(PyExc_ZeroDivisionError,
784786
"zero to a negative or complex power");

0 commit comments

Comments
 (0)