[Numpy-discussion] Casting rules - an awkward case

Matthew Brett matthew.brett@gmail....
Wed Apr 18 13:03:33 CDT 2012


Hi,

I just wanted to point out a situation where the scalar casting rules
can be a little confusing:

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?

See you,

Matthew


More information about the NumPy-Discussion mailing list