python / python/cpython

Spurious ZeroDivisionError for complex powers when the phase is infinite

Open
#156,886 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

>>> 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
  • gh-156887

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at _Py_c_pow() and complex_pow(), using the reproductions in the issue to observe the errno handling for infinite phases. Check that the result is nan+nanj rather than ZeroDivisionError, and run the relevant complex-power tests after confirming the linked PR's status.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.