python / python/cpython

Improve accuracy for complex powers with small negative integer exponents

未關閉
#156,695 1 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

interpreter-core type-feature
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Feature or enhancement

Proposal:

Currently we compute such powers as (1+0j)/z**(-n) (unless absolute value of n is too big to use specialized algorithm for integer exponents). This approach, however, introduce huge accuracy loss due to underflows in division.

For example:

>>> from gmpy2 import *                                                                                                                    
>>> import math                                                                                                                            
>>> x, y = map(float.fromhex, ['0x1.47e9c711723f5p+81',                                                                                    
...                            '0x1.38afd1168e49fp+85'])
>>> z = complex(x, y)                                                                                                                      
>>> pr = pow(z, -12); pr                                                                                                                   
0j
>>> pr2 = pow((1/z), 12); pr2                                                                                                              
(5.562684646267994e-309+5.56268464626799e-309j)
>>> gr = complex(pow(mpc(z), -12))                                                                                                         
>>> abs((pr2-gr).real)/math.ulp(gr.real)                                                                                                   
2.0
>>> abs((pr2-gr).imag)/math.ulp(gr.imag)                                                                                                   
2.0

Using instead ((1+0j)/z)**(-n) reduced error in this example from ~1e15 ULP to 2ULP. Note that, generic power algorithm is not affected by this issue:

>>> import _testcapi
>>> pr3 = _testcapi._py_c_pow(z, -12)[0]; pr3
(5.56268464626801e-309+5.56268464626799e-309j)
>>> abs((pr3-gr).real)/math.ulp(gr.real)
1.0
>>> abs((pr3-gr).imag)/math.ulp(gr.imag)
2.0
Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-156757
  • gh-156968

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先從 pow(z, -12) 和 _testcapi._py_c_pow 所執行的複數冪路徑開始,將它們與 pow((1/z), 12) 以及 reproducer 中顯示的 mpc 結果進行比較。完成的標準是避免範例中由 underflow 導致的精度損失;繼續之前請先檢視相關的 PR:gh-156757 和 gh-156968。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。