Skip to content

Spurious ZeroDivisionError for complex powers when the phase is infinite #156886

Description

@serhiy-storchaka
>>> 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.

Noticed by @hpkfft in #156694.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions