python / python/cpython

Spurious ZeroDivisionError for complex powers when the phase is infinite

未关闭
#156,886 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

>>> 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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 _Py_c_pow() 和 complex_pow() 开始,使用 issue 中的复现用例来观察对无限相位的 errno 处理。确认结果是 nan+nanj 而不是 ZeroDivisionError,并在确认所链接 PR 的状态后运行相关的复数幂测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。