thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

overflow not caught on operators with int32 array (Trac #2133) #5929

Open numpy-gitbot opened 12 years ago

numpy-gitbot commented 12 years ago

Original ticket http://projects.scipy.org/numpy/ticket/2133 on 2012-05-16 by trac user mwtoews, assigned to unknown.

I see good overflow warnings operations on int32 scalars, but not for int32 arrays:

import numpy as np

# Case 1: Good, proper data types are used to avoid overflow
np.array([1], dtype=np.long) + np.int32(2**31 - 1)
# array([2147483648], dtype=int64)

# Case 2: Bad, overflow happens and no warning raised
np.array([1], dtype=np.int32) + np.int32(2**31 - 1)
# array([-2147483648])

# Case 3: Similar bad, array vs. array
np.array([1], dtype=np.int32) + np.array([2**31 - 1], dtype=np.int32)
# array([-2147483648])

# Case 4: Better, a warning is raised
np.int32(1) + np.int32(2**31 - 1)
# __main__:1: RuntimeWarning: overflow encountered in long_scalars
# -2147483648

Here is what I can determine between operator op between arrays or scalars:

  1. array-long op scalar-32 = array-long, all good
  2. array-32 op scalar-32 = array-32, no overflow warning!
  3. array-32 op array-32 = array-32, no overflow warning!
  4. scalar-32 op scalar-32 = scalar-32, helpful overflow warning

I would expect an overflow runtime warning for cases 2 and 3, similar to case 4.

These results are with NumPy 1.6.1/Python 2.5.1 on Windows 32-bit, which I installed from numpy-unoptimized-1.6.1.win32-py2.5.exe from [http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy ~gohlke], but are reproducible on 64-bit Linux.

numpy-gitbot commented 12 years ago

atmention:rgommers wrote on 2012-05-19

Likely related to #1755.