Skip to content

ENH: Refactor numpy ** operators for numpy scalar integer powers #8231

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

Merged
merged 2 commits into from
Nov 5, 2016

Conversation

charris
Copy link
Member

@charris charris commented Nov 1, 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 discussion on the numpy mailing list where
it was decided that a numpy scalar integer raised to a negative integer
power should raise a ValueError, as do Numpy arrays. In particular, we
want numpy scalars and numpy 0-d arrays to have similar behavior.
Previously zero was returned for this case.
@charris
Copy link
Member Author

charris commented Nov 2, 2016

OK, I've done an almost minimal change for this PR. The whole scalarmath.c.src could use a cleanup, but that is for some other time.

@charris
Copy link
Member Author

charris commented Nov 4, 2016

Will merge soonish for 1.12.0.

@charris charris merged commit 2272c32 into numpy:master Nov 5, 2016
@charris charris deleted the integer-to-neg-integer-power branch November 5, 2016 16:29
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.

1 participant