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

Andrew Collette andrew.collette@gmail....
Thu Jan 3 21:35:57 CST 2013


Hi Olivier,

> Another solution is to forget about trying to be smart and always
> upcast the operation. That would be my 2nd preferred solution, but it
> would make it very annoying to deal with Python scalars (typically
> int64 / float64) that would be upcasting lots of things, potentially
> breaking a significant amount of existing code.
>
> So, personally, I don't see a straightforward solution without
> warning/error, that would be safe enough for programmers.

I guess what's really confusing me here is that I had assumed that this:

result = myarray + scalar

was equivalent to this:

result = myarray + numpy.array(scalar)

where the dtype of the converted scalar was chosen to be "just big
enough" for it to fit.  Then you proceed using the normal rules for
array addition.  Yes, you can have upcasting or rollover depending on
the values involved, but you have that anyway with array addition;
it's just how arrays work in NumPy.

Also, have I got this (proposed behavior) right?

array([127], dtype=int8) + 128 -> ValueError
array([127], dtype=int8) + 127 -> -2

It seems like all this does is raise an error when the current rules
would require upcasting, but still allows rollover for smaller values.
 What error condition, specifically, is the ValueError designed to
tell me about?   You can still get "unexpected" data (if you're not
expecting rollover) with no exception.

Andrew


More information about the NumPy-Discussion mailing list