Skip to content

MAINT, ENH: Refactor numpy ** operators for numpy scalar integer powers #8221

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

Closed
wants to merge 2 commits into from

Conversation

charris
Copy link
Member

@charris charris commented Oct 29, 2016

The previous behavior depended on whether numpy scalar integers or numpy integer arrays were involved.

For arrays

  • Zero to negative integer powers returned least integral value.
  • Both 1, -1 to negative integer powers returned correct values.
  • The remaining integers returned zero when raised to negative integer powers.

For scalars

  • Zero to negative integer powers returned least integral value.
  • Both 1, -1 to negative integer powers returned correct values.
  • The remaining integers sometimes returned zero, sometimes the correct float depending on the integer type combination.

All of these cases now raise a ValueError except for those integer combinations whose common type is float, for instance uint64 and int8. It was felt that a simple rule was the best way to go rather than have special exceptions for the integer units. If you need negative powers, use an inexact type.

Note that the np.power ufunc is used for everything. At this point I think it is more important to settle the behavior than optimize for speed. There are now some bits of code I left in that are currently unused.

This change is the upshot of a long discussion on the numpy mailing list
where is was decided that numpy scalar integers should raise a
ValueError when raised to a negative integer power. This behavior
differs from the Python case where the result is of float type, but
because numpy arrays cannot contain value dependent types and the desire
was to be consistent with numpy scalar arrays, we diverge from Python in
this regard. Previously numpy arrays would return zero in this case, but
because that is not a correct value they now also raise a ValueError.
@charris charris added this to the 1.12.0 release milestone Oct 29, 2016
@charris charris changed the title MAINT, ENH: Refactor numpy ** operators for integer powers MAINT, ENH: Refactor numpy ** operators for numpy scalar integer powers Oct 29, 2016
@charris
Copy link
Member Author

charris commented Oct 30, 2016

Don't merge yet, I'm going to refactor this.

@charris
Copy link
Member Author

charris commented Nov 1, 2016

Replaced by #8231, a more minimal patch. So closing this.

@charris charris closed this Nov 1, 2016
@homu
Copy link
Contributor

homu commented Nov 5, 2016

☔ The latest upstream changes (presumably #8240) made this pull request unmergeable. Please resolve the merge conflicts.

@charris charris deleted the scalar-power branch November 5, 2016 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants