>>> complex('inf')**1j
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
complex('inf')**1j
~~~~~~~~~~~~~~^^~~
ZeroDivisionError: zero to a negative or complex power
The base is not zero. The result should be nan+nanj, as in the GNU MPC.
_Py_c_pow() computes an infinite phase here, and cos() and sin() of an infinite argument are domain errors, so libm sets errno to EDOM. complex_pow() translates any EDOM to ZeroDivisionError, as it cannot distinguish it from the EDOM set for a zero base.
Any finite base for which the phase overflows is affected too:
>>> complex(1e300)**complex(0, 1e308)
ZeroDivisionError: zero to a negative or complex power
>>> complex(2)**complex(0, float('inf'))
ZeroDivisionError: zero to a negative or complex power
sin() and cos() set errno only for infinite arguments, so it is enough to discard errno in _Py_c_pow() after computing the result: _Py_ADJUST_ERANGE2() recovers ERANGE from the result itself.
A similar spurious ZeroDivisionError is raised when x**|n| underflows to zero for a negative integer exponent, e.g. (1e-162+0j)**-2. The cause is different -- _Py_rc_quot() with a zero denominator -- and it is not fixed by discarding errno in _Py_c_pow(). See gh-156695.
Activity
serhiy-storchaka commented on Sep 3, 2026
A similar spurious ZeroDivisionError is raised when x**|n| underflows to zero for a negative integer exponent, e.g. (1e-162+0j)**-2. The cause is different -- _Py_rc_quot() with a zero denominator -- and it is not fixed by discarding errno in _Py_c_pow(). See gh-156695.
gh-156886: Fix spurious ZeroDivisionError for complex powers (GH-156887)
pythongh-156886: Fix spurious ZeroDivisionError for complex powers (p…