<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/numpy/ticket/2133" rel="nofollow">http://projects.scipy.org/numpy/ticket/2133</a> on 2012-05-16 by trac user mwtoews, assigned to unknown.</em></p>
<p dir="auto">I see good overflow warnings operations on int32 scalars, but not for int32 arrays:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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"><pre class="notranslate"><code class="notranslate">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
</code></pre></div>
<p dir="auto">Here is what I can determine between operator <em>op</em> between arrays or scalars:</p>
<ol dir="auto">
<li>array-long <em>op</em> scalar-32 = array-long, all good</li>
<li>array-32 <em>op</em> scalar-32 = array-32, no overflow warning!</li>
<li>array-32 <em>op</em> array-32 = array-32, no overflow warning!</li>
<li>scalar-32 <em>op</em> scalar-32 = scalar-32, helpful overflow warning</li>
</ol>
<p dir="auto">I would expect an overflow runtime warning for cases 2 and 3, similar to case 4.</p>
<p dir="auto">These results are with NumPy 1.6.1/Python 2.5.1 on Windows 32-bit, which I installed from <code class="notranslate">numpy-unoptimized-1.6.1.win32-py2.5.exe</code> from [http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy ~gohlke], but are reproducible on 64-bit Linux.</p>
<p dir="auto">Simple demo:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [1]: np.uint8(0) - np.uint8(1)
RuntimeWarning: overflow encountered in ubyte_scalars
Out[1]: 255

In [2]: np.uint8(0)[...] - np.uint8(1)[...]
Out[2]: 255"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">1</span>]: <span class="pl-s1">np</span>.<span class="pl-en">uint8</span>(<span class="pl-c1">0</span>) <span class="pl-c1">-</span> <span class="pl-s1">np</span>.<span class="pl-en">uint8</span>(<span class="pl-c1">1</span>)
<span class="pl-v">RuntimeWarning</span>: <span class="pl-s1">overflow</span> <span class="pl-s1">encountered</span> <span class="pl-c1">in</span> <span class="pl-s1">ubyte_scalars</span>
<span class="pl-v">Out</span>[<span class="pl-c1">1</span>]: <span class="pl-c1">255</span>

<span class="pl-v">In</span> [<span class="pl-c1">2</span>]: <span class="pl-s1">np</span>.<span class="pl-en">uint8</span>(<span class="pl-c1">0</span>)[...] <span class="pl-c1">-</span> <span class="pl-s1">np</span>.<span class="pl-en">uint8</span>(<span class="pl-c1">1</span>)[...]
<span class="pl-v">Out</span>[<span class="pl-c1">2</span>]: <span class="pl-c1">255</span></pre></div>
<p dir="auto">Should we even warn at all for unsigned integers?</p>