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

Chris Barker - NOAA Federal chris.barker@noaa....
Fri Jan 18 00:32:55 CST 2013

On Thu, Jan 17, 2013 at 5:34 PM, Olivier Delalleau <shish@keba.be> wrote:
>> Yes, I do understand that.  The difference - as I understand it - is
>> that back in the day, numeric did not have the the float32 etc
>> scalars, so you could not do:
>> another_array = my_array * np.float32(4.0)
>> (please someone correct me if I'm wrong).

correct, it didn't have any scalars, but you could (and had to) still
do something like:

another_array = my_array * np.array(4.0, dtype=np.float32)

a bit more verbose, but the verbosity wasn't the key issue -- it was
doing anything special at all.

>>> Casual users will use the defaults that match the Python types anyway.
>> I think what we are reading in this thread is that even experienced
>> numpy users can find the scalar casting rules surprising, and that's a
>> real problem, it seems to me.

for sure -- but it's still relevant -- if you want non-default types,
you need to understand the rules an be more careful.

>> The person with a massive float32 array certainly should have the
>> ability to control upcasting, but I think the default should be the
>> least surprising thing, and that, it seems to me, is for the casting
>> rules to be the same for arrays and scalars.   In the very long term.

"A foolish consistency is the hobgoblin of little minds"

-- just kidding.

But in all seriousness -- accidental upcasting really was a big old
pain back in the day -- we are not making this up. We re using the
term "least surprising", but I now I was often surprised that I had
lost my nice compact array.

The user will need to think about it no matter how you slice it.

> Since most of the related issues seem to come from integer arrays, a
> middle-ground may be the following:
> - Integer-type arrays get upcasted by scalars as in usual array /
> array operations.
> - Float/Complex-type arrays don't get upcasted by scalars except when
> the scalar is complex and the array is float.

I'm not sure that integer arrays are any more of an an issue, and
having integer types and float typed behave differently is really
asking for trouble!



Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception


More information about the NumPy-Discussion mailing list