You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Re-examine complex pow special case handling #89133
bpo-15996: pow() for complex numbers is rough around the edges
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-10-22.08:23:37.519>created_at=<Date2021-08-21.13:26:43.363>labels= ['3.11']
title='Re-examine complex pow special case handling'updated_at=<Date2021-10-22.08:23:37.519>user='https://github.com/mdickinson'
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.
Activity
mdickinson commentedon Aug 21, 2021
Complex power, both via
**and the built-inpow, and viacmath.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
**andpowconsistent with that ofcmath.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:
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.
mdickinson commentedon Aug 21, 2021
Here's just one example that doesn't make a whole lot of sense: in this case, z ** 1 should be returning z.
mdickinson commentedon Aug 21, 2021
Aargh. Brain fail. There is no
cmath.pow, of course; this issue applies only to**and the built-inpow.serhiy-storchaka commentedon Aug 21, 2021
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.
serhiy-storchaka commentedon Oct 21, 2021
Is not it a duplicate of bpo-15996?
mdickinson commentedon Oct 22, 2021
Yes, I think it's close enough. Thanks.