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 — Specialize complex power for integer/real exponents · Issue #137746 · python/cpython

Specialize complex power for integer/real exponents #137746

New issue
Copy link
New issue
Copy link
Open
Open
Specialize complex power for integer/real exponents#137746
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 14, 2025
Issue body actions

Feature or enhancement

Proposal:

With #69639 complex arithmetic in Python was extended to support C-like mixed-more rules, where one operand is a float or an int. However, this doesn't affect exponentiation.

Currently we have, for example:

>>> import cmath
>>> z = complex('inf')
>>> z**0.5
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    z**0.5
    ~^^~~~
OverflowError: complex exponentiation
>>> cmath.sqrt(z)
(inf+0j)
>>> z**2
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    z**2
    ~^^~
OverflowError: complex exponentiation

which is just meaningless. Note also, that plain multiplication is not too much better in the last case:

>>> z*z
(inf+nanj)

What we actually want here is exp(log(z)*real_exponent):

>>> import cmath, math
>>> def mypow(b, e):
...     if isinstance(b, (float, int)) and b > 0:
...         if isinstance(e, (float, int)):
...             return math.exp(e*math.log(b))
...         return cmath.exp(e*math.log(b))
...     return cmath.exp(e*cmath.log(b))
...     
>>> mypow(z, 2)
(inf+0j)
>>> mypow(z+123j, 2)
(inf+0j)

This works as expected for real exponents and/or positive bases due to special mixed-mode rules in complex arithmetic (this time - in multiplication).

Special rules for exponentiation will be useful for implementation of complex functions, using known analytic identities. For example:

>>> cmath.asinh(z)
(inf+0j)
>>> myasinh_bad = lambda z: cmath.log(z + cmath.sqrt(1 + z**2))
>>> myasinh_bad(z)
Traceback (most recent call last):
  File "<python-input-11>", line 1, in <module>
    myasinh_bad(z)
    ~~~~~~~~~~~^^^
  File "<python-input-10>", line 1, in <lambda>
    myasinh_bad = lambda z: cmath.log(z + cmath.sqrt(1 + z**2))
                                                         ~^^~
OverflowError: complex exponentiation
>>> myasinh_bad2 = lambda z: cmath.log(z + cmath.sqrt(1 + z*z))
>>> myasinh_bad2(z)
(inf+nanj)
>>> myasinh_good = lambda z: cmath.log(z + cmath.sqrt(1 + mypow(z, 2)))
>>> myasinh_good(z)
(inf+0j)

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

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 14, 2025
  2. skirpichev
    self-assigned this
    on Aug 14, 2025
  3. picnixz commented on Aug 18, 2025

    @picnixz
    picnixz
    on Aug 18, 2025
    Member
    More actions

    What we actually want here is exp(log(z)*2)

    If Python could compute the principal value correctly in order to produce the correct value for $z^a$, then I'm in favor. Even if the C standard doesn't specify what to do, I think we should follow what Mathematica did (I'll need to check what Sage does as well)

  4. skirpichev commented on Aug 19, 2025

    @skirpichev
    skirpichev
    on Aug 19, 2025
    MemberAuthor
    More actions

    the C standard doesn't specify what to do

    Until recent drafts, it has no special case for complex division with lhs real op.

    I think we should follow what Mathematica did

    Yes, from https://reference.wolfram.com/language/ref/Power.html:

    For complex numbers x and y, Power gives the principal value of exp(y*log(x)).

    Though, I don't think that Mathematica has concept of signed zero.

  5. skirpichev commented on Aug 19, 2026

    @skirpichev
    skirpichev
    on Aug 19, 2026
    MemberAuthor
    More actions

    N.B., ISO/IEC 10967 (part 3) has special rules for RxR->C power function (§5.3.1.3).

  6. skirpichev
    removed their assignment
    on Nov 2, 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)(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.