[Numpy-discussion] Do we want scalar casting to behave as it does at the moment?

Olivier Delalleau shish@keba...
Tue Jan 8 15:29:45 CST 2013

2013/1/8 Chris Barker - NOAA Federal <chris.barker@noaa.gov>:
> On Tue, Jan 8, 2013 at 12:43 PM, Alan G Isaac <alan.isaac@gmail.com> wrote:
>>> New users don't use narrow-width dtypes... it's important to remember
>> 1. I think the first statement is wrong.
>> Control over dtypes is a good reason for
>> a new use to consider NumPy.
> Absolutely.
>> Because NumPy supports broadcasting,
>> it is natural for array-array operations and
>> scalar-array operations to be consistent.
>> I believe anything else will be too confusing.
> Theoretically true -- but in practice, the problem arrises because it
> is easy to write literals with the standard python scalars, so one is
> very likely to want to do:
> arr = np.zeros((m,n), dtype=np.uint8)
> arr += 3
> and not want an upcast.

Note that the behavior with in-place operations is also an interesting
topic, but slightly different, since there is no ambiguity on the
dtype of the output (which is required to match that of the input). I
was actually thinking about this earlier today but decided not to
mention it yet to avoid making the discussion even more complex ;)

The key question is whether the operand should be cast before the
operation, or whether to perform the operation in an upcasted array,
then downcast it back into the original version. I actually thnk the
latter makes more sense (and that's actually what's being done I think
in 1.6.1 from a few tests I tried), and to me this is an argument in
favor of the upcast behavior for non-inplace operations.

-=- Olivier

More information about the NumPy-Discussion mailing list