[Numpy-discussion] Integers to negative integer powers, time for a decision.

Nathaniel Smith njs at pobox.com
Sat Oct 8 19:34:30 EDT 2016

On Sat, Oct 8, 2016 at 3:18 PM, Krisztián Horváth <raksi.raksi at gmail.com> wrote:
>> but then that violates the numpy
>> principle that output dtypes should be determined entirely by input
>> dtypes, without peeking at the actual values. (And this rule is very
>> important for avoiding nasty surprises when you run your code on new
>> inputs.)
> At division you get back an array of floats.
>>>> y = np.int64([1,2,4])
>>>> y/1
> array([ 1.,  2.,  4.])
>>>> y/y
> array([ 1.,  1.,  1.])
> Why is it different, if you calculate the power of something?

The difference is that Python division always returns float. Python
int ** int sometimes returns int and sometimes returns float,
depending on which particular integers are used. We can't be
consistent with Python because Python isn't consistent with itself.

>> And then there's backwards compatibility to consider. As mentioned, we
>> *could* deviate from Python by making ** always return float... but
>> this would almost certainly break tons and tons of people's code that
>> is currently doing integer ** positive integer and expecting to get an
>> integer back. Which is something we don't do without very careful
>> weighing of the trade-offs, and my intuition is that this one is so
>> disruptive we probably can't pull it off. Breaking working code needs
>> a *very* compelling reason.
> This is a valid reasoning. But it could be solved with raising an exception
> to warn the users for the new behaviour.

That is generally the best conservative strategy for making a
backwards incompatible change like this: instead of going straight to
the new behavior, first make it raise an error, and then once people
have had time to stop depending on the old behavior, then you can add
the new behavior. But in this case if we were going to make int ** int
return float, this rule would mean that we have to make int ** int
always raise an error for a few years, i.e. remove integer power
support from numpy altogether. That's a non-starter.


Nathaniel J. Smith -- https://vorpus.org

More information about the NumPy-Discussion mailing list