On Wed, 2022-09-28 at 16:44 -0700, Stefan van der Walt wrote:
> Hi Sebastian,
> 
> On Wed, Sep 28, 2022, at 12:11, Sebastian Berg wrote:
> >     np.array([1, 2], dtype="uint8") + (-1)
> > 
> > which currently returns an "int16" array must raise an error.  This
> > is
> > because we want to return a uint8 array but the -1 cannot be
> > represented well by -1.
> 
> Did you mean: the -1 is not representable in uint8?

Sorry yes.  With NEP 50, we do not look at the value (initially) so
determine that the operation must be handled as:

    uint8 + uint8 -> uint8

We then try to convert the -1 to uint8.  That conversion would raise an
error, because previously the result was an int16.
(This is to prevent silent unexpected result changes and seemed like
the more reasonable behavior.)

On the other hand, assignments like:

    uint8_arr[0] = -1
    np.array([-1], dtype=np.uint8)

do happily convert the -1 to `uint8`, currently.  If we keep allowing
these, we have two slightly different conversions:  one that fails and
one that does not.

This is fine, but maybe we actually want it to always fail in the
future?

- Sebastian


> 
> Since -1 cannot cast to uint8, and since we cannot look at the value,
> we cannot determine a suitable minimal output dtype for x - 1.
> 
> Stéfan
> _______________________________________________
> NumPy-Discussion mailing list -- numpy-discussion@python.org
> To unsubscribe send an email to numpy-discussion-le...@python.org
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: sebast...@sipsolutions.net


_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to