GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve accuracy for complex powers with small negative integer exponents · Issue #156695 · python/cpython

Improve accuracy for complex powers with small negative integer exponents #156695

New issue
Copy link
New issue
Copy link
Open
#156757
Open
Improve accuracy for complex powers with small negative integer exponents#156695
#156757
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

Description

@skirpichev
skirpichev
opened on Aug 31, 2026
Issue body actions

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-156695: Improve accuracy of complex powers with small negative integer exponents #156757
  • gh-156695: Improve accuracy of complex div and pow #156968
Reacted by Aniket

Activity

  1. skirpichev
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Aug 31, 2026
  2. Aniketsy
    added a commit that references this issue on Sep 1, 2026

    pythongh-156695: Improve accuracy of complex powers with small negati…

    c0eeaee
  3. serhiy-storchaka commented on Sep 3, 2026

    @serhiy-storchaka
    serhiy-storchaka
    on Sep 3, 2026
    Member
    More actions

    There is also the case when x**|n| underflows to zero:

    >>> (1e-161+0j)**-2
    Traceback (most recent call last):
      ...
    OverflowError: complex exponentiation
    >>> (1e-162+0j)**-2
    Traceback (most recent call last):
      ...
    ZeroDivisionError: zero to a negative or complex power

    OverflowError is correct in both cases, the base is not zero. The scaling in #156757 would give it, but it is skipped here because of the errno != EDOM condition.

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)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.