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

Olivier Delalleau shish at keba.be
Fri Jan 4 11:34:34 EST 2013


(sorry, no time for full reply, so for now just answering what I
believe is the main point)

2013/1/4 Andrew Collette <andrew.collette at gmail.com>:
>> The ValueError is here to warn you that the operation may not be doing
>> what you want. The rollover for smaller values would be the documented
>> (and thus hopefully expected) behavior.
>
> Right, but what confuses me is that the only thing this prevents is
> the current upcast behavior.  Why is that so evil it should be
> replaced with an exception?

The evilness lies in the silent switch between the rollover and upcast
behavior, as in the example I gave previously:

    In [50]: np.array([2], dtype='int8') + 127
    Out[50]: array([-127], dtype=int8)
    In [51]: np.array([2], dtype='int8') + 128
    Out[51]: array([130], dtype=int16)

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.

Looking at what other numeric libraries are doing is definitely a good
suggestion.

-=- Olivier



More information about the NumPy-Discussion mailing list