This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Re-examine complex pow special case handling
Type: Stage: resolved
Components: Versions: Python 3.11
process
Status: closed Resolution: duplicate
Dependencies: Superseder: pow() for complex numbers is rough around the edges
View: 15996
Assigned To: Nosy List: mark.dickinson, serhiy.storchaka
Priority: low Keywords:

Created on 2021-08-21 13:26 by mark.dickinson, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg400025 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-08-21 13:26
Complex power, both via `**` and the built-in `pow`, and via `cmath.pow`, is currently a bit of a mess when it comes to special-case handling - particularly handling of signed zeros, infinities, NaNs, and overflow.

At some point it would be nice to rationalise and document the special-case handling, as far as possible, and to make the behaviour of `**` and `pow` consistent with that of `cmath.pow`. Note that while for all the other cmath functions we have good guidance from the C standards on how special cases should be handled, for pow we're on our own - the C standard refuses to specify anything at all about special case handling.

Note also that there are a *lot* of special cases to consider. We have four real input parameters (the real and imaginary parts of each of the base and the exponent), each of which can be one of the 7 cases nan, -inf, -finite, -0.0, 0.0, finite, inf, for a total of 7**4 = 2401 combinations; moreover, for some cases we might need to distinguish integral from non-integral values, and even integers from odd integers.

This is low priority - in many years of mathematical, scientific and numeric work, I've seen little evidence that anyone actually cares about or uses general complex power. Most users are interested in one or more subcases, like:

- positive real base and complex exponent
- complex base and integral exponent
- complex nth root for positive integers n, especially for small n (square root, cube root, ...)

So a possibly more manageable and more useful subtask would be to ensure that special cases are handled in a sensible manner for these subcases.
msg400026 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-08-21 13:31
Here's just one example that doesn't make a whole lot of sense: in this case, z ** 1 should be returning z.

>>> z = complex(3, float("inf"))
>>> z ** 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: complex exponentiation
msg400027 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-08-21 13:33
Aargh. Brain fail. There *is* no `cmath.pow`, of course; this issue applies only to `**` and the built-in `pow`.
msg400037 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-21 17:24
The OverflowError should only be raised if finite arguments produce infinite result. Py_ADJUST_ERANGE2 is used improperly.

But after fixing this error I get (nan+infj) for z**1, the same as for z*1.
msg404654 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-21 20:45
Is not it a duplicate of issue15996?
msg404726 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-10-22 08:23
> Is not it a duplicate of issue15996?

Yes, I think it's close enough. Thanks.
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89133
2021-10-22 08:23:37mark.dickinsonsetstatus: open -> closed
superseder: pow() for complex numbers is rough around the edges
messages: + msg404726

resolution: duplicate
stage: resolved
2021-10-21 20:45:50serhiy.storchakasetmessages: + msg404654
2021-08-21 17:24:42serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg400037
2021-08-21 13:33:48mark.dickinsonsetmessages: + msg400027
2021-08-21 13:31:47mark.dickinsonsetmessages: + msg400026
2021-08-21 13:26:43mark.dickinsoncreate