Skip to content

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

Merged
charris merged 2 commits into
numpy:masterfrom
charris:integer-to-neg-integer-power
Nov 5, 2016
Merged

charris merged 2 commits into
numpy:masterfrom
charris:integer-to-neg-integer-power

Conversation

@charris

@charris charris commented Nov 1, 2016

Copy link
Copy Markdown
Member

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

charris commented Nov 2, 2016

Copy link
Copy Markdown
Member Author

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

charris commented Nov 4, 2016 •

Copy link
Copy Markdown
Member Author

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