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

Nathaniel Smith njs at pobox.com
Sat Oct 8 11:12:31 EDT 2016


On Sat, Oct 8, 2016 at 6:59 AM, Charles R Harris
<charlesr.harris at gmail.com> wrote:
>
>
> On Sat, Oct 8, 2016 at 4:40 AM, Nathaniel Smith <njs at pobox.com> wrote:
>>
>> On Fri, Oct 7, 2016 at 6:12 PM, Charles R Harris
>> <charlesr.harris at gmail.com> wrote:
>> > Hi All,
>> >
>> > The time for NumPy 1.12.0 approaches and I like to have a final decision
>> > on
>> > the treatment of integers to negative integer powers with the `**`
>> > operator.
>> > The two alternatives looked to be
>> >
>> > Raise an error for arrays and numpy scalars, including 1 and -1 to
>> > negative
>> > powers.
>> >
>> > Pluses
>> >
>> > Backward compatible
>> > Allows common powers to be integer, e.g., arange(3)**2
>> > Consistent with inplace operators
>> > Fixes current wrong behavior.
>> > Preserves type
>> >
>> >
>> > Minuses
>> >
>> > Integer overflow
>> > Computational inconvenience
>> > Inconsistent with Python integers
>> >
>> >
>> > Always return a float
>> >
>> > Pluses
>> >
>> > Computational convenience
>> >
>> >
>> > Minuses
>> >
>> > Loss of type
>> > Possible backward incompatibilities
>> > Not applicable to inplace operators
>>
>> I guess I could be wrong, but I think the backwards incompatibilities
>> are going to be *way* too severe to make option 2 possible in
>> practice.
>>
>
> Backwards compatibility is also a major concern for me.  Here are my current
> thoughts
>
> Add an fpow ufunc that always converts to float, it would not accept object
> arrays.

Maybe call it `fpower` or even `float_power`, for consistency with `power`?

> Raise errors in current power ufunc (**), for ints to negative ints.
>
> The power ufunc will change in the following ways
>
> +1, -1 to negative ints will error, currently they work
> n > 1 ints to negative ints will error, currently warn and return zero
> 0 to negative ints will error, they currently return the minimum integer
>
> The `**` operator currently calls the power ufunc, leave that as is for
> backward almost compatibility. The remaining question is numpy scalars,
> which we can make either compatible with Python, or with NumPy arrays. I'm
> leaning towards NumPy array compatibility mostly on account of type
> preservation and the close relationship between zero dimensionaly arrays and
> scalars.

Sounds good to me. I agree that we should prioritize within-numpy
consistency over consistency with Python.

> The fpow function could be backported to NumPy 1.11 if that would be helpful
> going forward.

I'm not a big fan of this kind of backport. Violating the
"bug-fixes-only" rule makes it hard for people to understand our
release versions. And it creates the situation where people can write
code that they think requires numpy 1.11 (because it works with their
numpy 1.11!), but then breaks on other people's computers (because
those users have 1.11.(x-1)). And if there's some reason why people
aren't willing to upgrade to 1.12 for new features, then probably
better to spend energy addressing those instead of on putting together
1.11-and-a-half releases.

-n

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



More information about the NumPy-Discussion mailing list