Erdaron

February 10th, 2016, 05:26 PM

I ran into an issue where numpy's numerical types don't seem to be well-behaved. Consider the following sequence of commands:

>>> type(np.uint64(5))

<type 'numpy.uint64'>

>>> type(np.uint64(5) + 5)

<type 'numpy.float64'>

>>> type(long(5))

<type 'long'>

>>> type(long(5) + 5)

<type 'long'>

>>> type(np.uint32(5) + 5)

<type 'numpy.int64'>

>>> type(np.int32(5) + 5)

<type 'numpy.int32'>

>>> type(np.int64(5) + 5)

<type 'numpy.int64'>

Basically, it seems that numpy's unsigned integer types get recast when mixed with native Python types. Unsigned 32-bit integer gets converted into signed 64-bit integer, and unsigned 64-bit integer into 64-bit float. Signed integer types are safe.

The latter conversion is of particular concern, because it can potentially wreck things.

Python 2.7.10 x64, numpy 1.10.4 x64 with MKL support (from Christoph Gohlke repo), on Windows 7 x64.

Am I missing some nuance here? Is there an order in which things get re-cast when mixed types are encountered?

>>> type(np.uint64(5))

<type 'numpy.uint64'>

>>> type(np.uint64(5) + 5)

<type 'numpy.float64'>

>>> type(long(5))

<type 'long'>

>>> type(long(5) + 5)

<type 'long'>

>>> type(np.uint32(5) + 5)

<type 'numpy.int64'>

>>> type(np.int32(5) + 5)

<type 'numpy.int32'>

>>> type(np.int64(5) + 5)

<type 'numpy.int64'>

Basically, it seems that numpy's unsigned integer types get recast when mixed with native Python types. Unsigned 32-bit integer gets converted into signed 64-bit integer, and unsigned 64-bit integer into 64-bit float. Signed integer types are safe.

The latter conversion is of particular concern, because it can potentially wreck things.

Python 2.7.10 x64, numpy 1.10.4 x64 with MKL support (from Christoph Gohlke repo), on Windows 7 x64.

Am I missing some nuance here? Is there an order in which things get re-cast when mixed types are encountered?