title: Unexpected Numpy / Py3k coercion rules
body: <p>I was looking for a bug in a program, and I discovered that it was produced by an unexpected behavior from Numpy...</p>
  
  <p>When doing, e.g., a simple arithmetic operation on different integer types using Python3k and Numpy, like</p>
  
  <p>(numpy.uint64) + (int)</p>
  
  <p>the result is... a numpy.float64</p>
  
  <p>Here's an example:</p>
  
  <pre><code>v = numpy.array([10**16+1], dtype=numpy.uint64)
  print(v[0])
  v[0] += 1
  print(v[0])
  </code></pre>
  
  <p>It produce the following result :</p>
  
  <pre><code>10000000000000001
  10000000000000000
  </code></pre>
  
  <p>Which can be quite unexpected when you're dealing with integers to avoid rounding errors...</p>
  
  <p>The above "problem" can easily be solved by replacing 1 by numpy.uint64(1), but I can see many bugs coming from this. What are the rules and logic behind this situation? Is there any documentation about the way coercions are done in such a case? I couldn't find it.</p>
  
  <p>I thought before that you could have some insight on the coercions by using .item() but it's even more misleading :</p>
  
  <pre><code>v = numpy.array([10**16+1], dtype=numpy.uint64)
  print(type(v[0].item()))
  v[0] = v[0].item() + 1
  print(v[0])
  </code></pre>
  
  <p>produces</p>
  
  <pre><code>&lt;class 'int'&gt;
  10000000000000001
  10000000000000002
  </code></pre>
  
  <p>So .item() transforms the numpy.uint64 into int, and if you explicitely use it in the arithmetic operation, it works.</p>
  
  <p>I'm surprised (but I lack numpy experience, I guess), that, when 'a' corresponds to a numpy specific dtype,</p>
  
  <pre><code>a.item() + 1
  </code></pre>
  
  <p>and</p>
  
  <pre><code>a + 1
  </code></pre>
  
  <p>don't produce the same results... and thus gives different results when converted back to a numpy dtype.</p>
  
  <p>(The environment used is an up-to-date Pyzo distribution, via IEP, if it matters. I usually use Python 2, but I had to do a couple test in Py3k, and it was a convenient way to do it.)</p>