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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Sep 3, 2026
  2. serhiy-storchaka commented on Sep 3, 2026

    @serhiy-storchaka
    MemberAuthor

    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.

  3. added a commit that references this issue on Sep 4, 2026
  4. added a commit that references this issue on Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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