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 — gh-156693: correct complex powers with infinite bases by skirpichev · Pull Request #156694 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Aug 31, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Now we raise OverflowError's only if infinite result (i.e. cmath.isinf(result) is True) come from finite input.

This patch also adds private inline functions _Py_c_iszero(), _Py_c_isnan(), _Py_c_isinf() and _Py_c_isfinite().

  • Issue: Incorrect overflows for complex powers when base is infinite #156693

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@skirpichev
pythongh-156693: correct complex powers with infinite bases
42391fe
Now we raise OverflowError's only if infinite result (i.e.
cmath.isinf(result) is True) come from finite input.  As proposed in
issue python#156145, we don't use platforms `errno` to manage exceptions
anymore.

This patch also adds private API functions `_Py_c_isnan()`,
`_Py_c_isinf()` and `_Py_c_isfinite()`.
@skirpichev
skirpichev requested a review from serhiy-storchaka August 31, 2026 05:01
@bedevere-app bedevere-app Bot mentioned this pull request Aug 31, 2026
Incorrect overflows for complex powers when base is infinite #156693
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Aug 31, 2026
@skirpichev

skirpichev commented Aug 31, 2026

Copy link
Copy Markdown
Member Author

CC @serhiy-storchaka

I think this will be less controversial change vs #118000 or #124243. Anyway, I think it worth for a dedicated commit.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Aug 31, 2026

Copy link
Copy Markdown
Member Author

CC @hpkfft

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

skirpichev
skirpichev commented Aug 31, 2026
View reviewed changes
Comment thread Objects/complexobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
Drop unused _Py_ADJUST_ERANGE2 macro
c93746a
This was referenced Aug 31, 2026
gh-156145: Don't rely on errno for float pow and is_integer #156551
Open
gh-155526: correct errno handling in complex_abs() #155527
Merged
eendebakpt
eendebakpt requested changes Sep 1, 2026
View reviewed changes
Comment thread Include/internal/pycore_complexobject.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-08-31-07-33-05.gh-issue-156693.BuDcDc.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Objects/complexobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting changes and removed awaiting review labels Sep 1, 2026
@bedevere-app

bedevere-app Bot commented Sep 1, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

skirpichev and others added 4 commits September 1, 2026 14:39
@skirpichev @eendebakpt
Apply suggestion from @eendebakpt
9ce4cc8
Co-authored-by: Pieter Eendebak <[email protected]>
@skirpichev
Merge branch 'master' into fix-cpow-overflows/156693
f7b9417
@skirpichev
address review: add c_quot
3c4878c
@skirpichev
address review: inline new helpers
eb7bdf7
@skirpichev
skirpichev requested a review from eendebakpt September 1, 2026 13:10
@skirpichev

skirpichev commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

I have made the requested changes; please review again

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting change review and removed awaiting changes labels Sep 1, 2026
@bedevere-app

bedevere-app Bot commented Sep 1, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@eendebakpt: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

eendebakpt
eendebakpt reviewed Sep 1, 2026
View reviewed changes

@eendebakpt eendebakpt left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more caseswhere the behavior changes: (1e-200+0j)**-2 (it would not happen under #156695, but that is not merged yet).

And can you add a test for complex(inf)**1j?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-08-31-07-33-05.gh-issue-156693.BuDcDc.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

skirpichev and others added 3 commits September 2, 2026 02:23
@skirpichev @eendebakpt
Update Misc/NEWS.d/next/Core_and_Builtins/2026-08-31-07-33-05.gh-issu…
af33ff4
…e-156693.BuDcDc.rst

Co-authored-by: Pieter Eendebak <[email protected]>
@skirpichev
Merge branch 'master' into fix-cpow-overflows/156693
256a454
@skirpichev
address review: correct zero division case and add inf**1j test
ee0a74f
@skirpichev

skirpichev commented Sep 2, 2026

Copy link
Copy Markdown
Member Author

One more caseswhere the behavior changes: (1e-200+0j)**-2

Indeed, I believe now all should be fixed.

I'll not add a test, though, because this behavior can be changed by #156695.

And can you add a test for complex(inf)**1j?

Done.

Last commit removes special handling for some corner cases, that's optional. Let me know if you prefer to keep this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
optional: drop corner cases (should be handled by floating-point arit…
935b0e6
…hmetics)
@skirpichev
skirpichev force-pushed the fix-cpow-overflows/156693 branch from 8e7ff19 to 935b0e6 Compare September 2, 2026 02:02
@eendebakpt

eendebakpt commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

With your latest iteration: what about (0j)**complex(INF), (1e-200+0j)**-2 and _Py_c_pow(nan+0j, 0+0j) (C API)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
skirpichev marked this pull request as draft September 2, 2026 10:41
@bedevere-app bedevere-app Bot removed the awaiting change review label Sep 2, 2026
@skirpichev skirpichev self-assigned this Sep 2, 2026
@hpkfft

hpkfft commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

In my opinion, anybody calling _Py_c_pow() (e.g., Numba) should get the same results as a Python developer calling pow(). The latter has an optimization for small integer exponents. Numba should be allowed to benefit from that.

You're welcome to look at my work-in-progress: hpkfft#1
I deleted c_powu(). Now, complex_pow() only calls c_pow() and raises a Python error if necessary. The C API function _Py_c_pow() also calls c_pow() and sets errno if necessary. If small integer exponent, c_pow() calls c_powi(), so the C API and the Python API give the same results.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

skirpichev added 7 commits September 3, 2026 04:55
@skirpichev
Revert "optional: drop corner cases (should be handled by floating-po…
fec072f
…int arithmetics)"

This reverts commit 935b0e6.
@skirpichev
Merge branch 'master' into fix-cpow-overflows/156693
adde55b
@skirpichev
keep only basic fix
ad845c1
@skirpichev
+ fix test
ee01d1f
@skirpichev
+ few more tests
aa85e0b
@skirpichev
+ more cases for _Py_c_iszero
27bca7f
@skirpichev
+ correct news
8da530c
@skirpichev

skirpichev commented Sep 3, 2026

Copy link
Copy Markdown
Member Author

In my opinion, anybody calling _Py_c_pow() (e.g., Numba) should get the same results as a Python developer calling pow().

Anyone, calling _Py_c_pow() should be aware, that it's a soft-deprecated interface.

We decided to not provide low-level primitives, that works same as complex arithmetics in the Python. You can use either PyNumber_* API or some external library code (e.g. like GNU GSL) and/or C types (e.g. double _Complex, if available).

You're welcome to look at my work-in-progress: hpkfft#1

Thanks, but I'll try to limit this PR just for a bugfix.

IMO, cleanest alternative to using errno is floating-point exceptions. Though, my tests shows that this come with a severe speed loss. Lets decide on this later.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
skirpichev requested a review from eendebakpt September 3, 2026 03:00
@skirpichev
skirpichev marked this pull request as ready for review September 3, 2026 03:00
@bedevere-app bedevere-app Bot added the awaiting review label Sep 3, 2026
@skirpichev
+ restore _Py_ADJUST_ERANGE2
ae4c396
@skirpichev

skirpichev commented Sep 3, 2026

Copy link
Copy Markdown
Member Author

Ah, it seems that added complex('inf')**1j test is still in mercy of platforms libm. Test pass for me, but breaks CI. I'll revert it.

BTW, I'm not sure that ZeroDivisionError is a right thing here. On latest gmpy2:

>>> from gmpy2 import *
>>> get_context().trap_divzero = True
>>> mpc('inf')**mpc(1j)
mpc('nan+nanj')
>>> get_context()
context(precision=53, real_prec=Default, imag_prec=Default,
        round=RoundToNearest, real_round=Default, imag_round=Default,
        emax=1073741823, emin=-1073741823,
        subnormalize=False,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=False,
        trap_invalid=False, invalid=True,
        trap_erange=False, erange=False,
        trap_divzero=True, divzero=False,
        allow_complex=False,
        rational_division=False,
        allow_release_gil=False)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
drop flacky test
5b9607d
@hpkfft

hpkfft commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

I'm not sure that ZeroDivisionError is a right thing here.

My preference would be ZeroDivisionError only if base is zero and exp is not a positive real number.
The following seems obviously wrong:

>>> import math
>>> c = complex(math.inf)
>>> c**1j
Traceback (most recent call last):
  File "<python-input-7>", line 1, in <module>
    c**1j
    ~^^~~
ZeroDivisionError: 0.0 to a negative or complex power
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev skirpichev closed this Sep 3, 2026
@skirpichev
skirpichev deleted the fix-cpow-overflows/156693 branch September 3, 2026 08:49
@serhiy-storchaka serhiy-storchaka mentioned this pull request Sep 3, 2026
Spurious ZeroDivisionError for complex powers when the phase is infinite #156886
Closed
@serhiy-storchaka

serhiy-storchaka commented Sep 3, 2026

Copy link
Copy Markdown
Member

Opened #156886 for ZeroDivisionError.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt

eendebakpt commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

@skirpichev Do you plan to followup on this PR? Some of the added tests seem worthwhile to have in any case.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Sep 4, 2026

Copy link
Copy Markdown
Member Author

Some of the added tests seem worthwhile to have in any case.

All are about corner cases, with special components (like inf or zero). We have a dedicated issue #60200 about that.

(1e-200+0j)**-2 might be added to #156695.

Let me know if you think it worth to add predicates (_Py_c_isnan() and co).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hpkfft hpkfft mentioned this pull request Sep 5, 2026
gh-156695: Improve accuracy of complex div and pow #156968
Draft
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka Awaiting requested review from serhiy-storchaka
@eendebakpt eendebakpt Awaiting requested review from eendebakpt

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@skirpichev skirpichev

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@skirpichev @eendebakpt @hpkfft @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — 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.