Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Doc/c-api/complex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pointers. This is consistent throughout the API.
If *num* is null and *exp* is not a positive real number,
this method returns zero and sets :c:data:`errno` to :c:macro:`!EDOM`.

Set :c:data:`errno` to :c:macro:`!ERANGE` on overflows.


Complex Numbers as Python Objects
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_capi/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ def test_py_c_pow(self):

self.assertEqual(_py_c_pow(0j, -1)[1], errno.EDOM)
self.assertEqual(_py_c_pow(0j, 1j)[1], errno.EDOM)
self.assertEqual(_py_c_pow(*[DBL_MAX+1j]*2)[0], complex(*[INF]*2))
max_num = DBL_MAX+1j
self.assertEqual(_py_c_pow(max_num, max_num),
(complex(INF, INF), errno.ERANGE))
self.assertEqual(_py_c_pow(max_num, 2),
(complex(INF, INF), errno.ERANGE))


def test_py_c_abs(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Set :data:`errno` in :c:func:`_Py_c_pow` on overflows. Patch by Sergey B
Kirpichev.
4 changes: 3 additions & 1 deletion Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ _Py_c_pow(Py_complex a, Py_complex b)
}
r.real = len*cos(phase);
r.imag = len*sin(phase);

_Py_ADJUST_ERANGE2(r.real, r.imag);
}
return r;
}
Expand Down Expand Up @@ -546,12 +548,12 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
// a faster and more accurate algorithm.
if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= 100.0) {
p = c_powi(a, (long)b.real);
_Py_ADJUST_ERANGE2(p.real, p.imag);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be moved to c_powi(), but that will make patch slightly larger. I think this is fine, as c_powi() is not a part of C API.

}
else {
p = _Py_c_pow(a, b);
}

_Py_ADJUST_ERANGE2(p.real, p.imag);
if (errno == EDOM) {
PyErr_SetString(PyExc_ZeroDivisionError,
"zero to a negative or complex power");
Expand Down