[Numpy-discussion] Proposal to make power return float, and other such things.

Robert Kern robert.kern at gmail.com
Tue Feb 18 05:55:20 EST 2014

On Tue, Feb 18, 2014 at 9:51 AM, Sturla Molden <sturla.molden at gmail.com> wrote:
> Charles R Harris <charlesr.harris at gmail.com> wrote:
>> This is apropos issue #899 <<a
>> href="https://github.com/numpy/numpy/issues/899">https://github.com/numpy/numpy/issues/899</a>>,
>> where it is suggested that power promote integers to float. That sounds
>> reasonable to me, but such a change in behavior makes it a bit iffy.
>> Thoughts?
> Numpy should do the same as Python does.

That's problematic because Python's behavior is value dependent.

Python 3.3.1 (default, May 16 2013, 17:20:13)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 ** 2
>>> 2 ** -2

That's fine if you only have one value for each operand. When you have
multiple values for each operand, say an exponent array containing
both positive and negative integers, that becomes a problem.
Generally, we try to make ufuncs return types that are predictable
from the types of the operands, not the values of the operands.

I am -1 on the proposal to make power(x:int, y:int) always return a
float. It is usually trivial to just make the exponent a float if one
wants a float returned. Or we could introduce an fpow() that always
coerces the inputs to the best inexact type.

Robert Kern

More information about the NumPy-Discussion mailing list