[Numpy-discussion] Casting rules - an awkward case

Matthew Brett matthew.brett@gmail....
Wed Apr 18 14:05:36 CDT 2012


Oops, sorry, Keith Goodman kindly pointed out that I had missed out:

On Wed, Apr 18, 2012 at 11:03 AM, Matthew Brett <matthew.brett@gmail.com> wrote:
> Hi,
>
> I just wanted to point out a situation where the scalar casting rules
> can be a little confusing:

In [110]: a = np.array([-128, 127], dtype=np.int8)

> In [113]: a - np.int16(128)
> Out[113]: array([-256,   -1], dtype=int16)
>
> In [114]: a + np.int16(-128)
> Out[114]: array([ 0, -1], dtype=int8)
>
> This is predictable from the nice docs here:
>
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.result_type.html
>
> but I offer it only as a speedbump I hit.
>
> On the other hand I didn't find it easy to predict what numpy 1.5.1
> was going to do:
>
> In [31]: a - np.int16(1)
> Out[31]: array([127, 126], dtype=int8)
>
> In [32]: a + np.int16(-1)
> Out[32]: array([-129,  126], dtype=int16)
>
> As a matter of interest, what was the rule for 1.5.1?

Matthew


More information about the NumPy-Discussion mailing list