Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-examine complex pow special case handling #89133

Closed
mdickinson opened this issue Aug 21, 2021 · 6 comments
Closed

Re-examine complex pow special case handling #89133

mdickinson opened this issue Aug 21, 2021 · 6 comments
Labels
3.11 only security fixes

Comments

@mdickinson
Copy link
Member

BPO 44970
Nosy @mdickinson, @serhiy-storchaka
Superseder
  • 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 = None
    closed_at = <Date 2021-10-22.08:23:37.519>
    created_at = <Date 2021-08-21.13:26:43.363>
    labels = ['3.11']
    title = 'Re-examine complex pow special case handling'
    updated_at = <Date 2021-10-22.08:23:37.519>
    user = 'https://github.com/mdickinson'

    bugs.python.org fields:

    activity = <Date 2021-10-22.08:23:37.519>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-22.08:23:37.519>
    closer = 'mark.dickinson'
    components = []
    creation = <Date 2021-08-21.13:26:43.363>
    creator = 'mark.dickinson'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44970
    keywords = []
    message_count = 6.0
    messages = ['400025', '400026', '400027', '400037', '404654', '404726']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'serhiy.storchaka']
    pr_nums = []
    priority = 'low'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '15996'
    type = None
    url = 'https://bugs.python.org/issue44970'
    versions = ['Python 3.11']

    @mdickinson
    Copy link
    Member Author

    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.

    @mdickinson mdickinson added 3.11 only security fixes labels Aug 21, 2021
    @mdickinson
    Copy link
    Member Author

    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

    @mdickinson
    Copy link
    Member Author

    Aargh. Brain fail. There is no cmath.pow, of course; this issue applies only to ** and the built-in pow.

    @serhiy-storchaka
    Copy link
    Member

    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
    Copy link
    Member

    Is not it a duplicate of bpo-15996?

    @mdickinson
    Copy link
    Member Author

    Is not it a duplicate of bpo-15996?

    Yes, I think it's close enough. Thanks.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants