[Numpy-discussion] 16bit Integer Array/Scalar Inconsistency

Ryan May rmay@ou....
Thu Aug 2 14:18:49 CDT 2007


I ran into this while debugging a script today:

In [1]: import numpy as N

In [2]: N.__version__
Out[2]: '1.0.3'

In [3]: d = N.array([32767], dtype=N.int16)

In [4]: d + 32767
Out[4]: array([-2], dtype=int16)

In [5]: d[0] + 32767
Out[5]: 65534

In [6]: type(d[0] + 32767)
Out[6]: <type 'numpy.int64'>

In [7]: type(d[0])
Out[7]: <type 'numpy.int16'>

It seems that numpy will automatically promote the scalar to avoid
overflow, but not in the array case.  Is this inconsistency a bug, just
a (known) gotcha?

I myself don't have any problems with the array not being promoted
automatically, but the inconsistency with scalar operation made
debugging my problem more difficult.


Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

More information about the Numpy-discussion mailing list