[Numpy-discussion] Do we want scalar casting to behave as it does at the moment?
Fri Jan 4 10:57:05 CST 2013
> (sorry, no time for full reply, so for now just answering what I
> believe is the main point)
Thanks for taking the time to discuss/explain this at all... I appreciate it.
> The evilness lies in the silent switch between the rollover and upcast
> behavior, as in the example I gave previously:
> In : np.array(, dtype='int8') + 127
> Out: array([-127], dtype=int8)
> In : np.array(, dtype='int8') + 128
> Out: array(, dtype=int16)
Right, but for better or for worse this is how *array* addition works.
If I have an int16 array in my program, and I add a user-supplied
array to it, I get rollover if they supply an int16 array and
upcasting if they provide an int32. The answer may simply be that we
consider scalar addition a special case; I think that's really what
tripping me up here.
Granted, one is a type-dependent change while the other is a
value-dependent change; but in my head they were connected by the
rules for choosing a "effective" dtype for a scalar based on its
> If the scalar is the user-supplied value, it's likely you actually
> want a fixed behavior (either rollover or upcast) regardless of the
> numeric value being provided.
This is a good point; thanks.
> Looking at what other numeric libraries are doing is definitely a good
I just double-checked IDL, and for addition it seems to convert to the
a = bytarr(10)
<Expression> INT = Array
<Expression> LONG = Array
Of course, IDL and Python scalars likely work differently.
More information about the NumPy-Discussion