title: Sum of positive numbers results in a negative number
body: <p>I am using numpy to do the always fun "count the triangles in an adjacency matrix" task.  (<a href="https://stackoverflow.com/questions/17740081/given-an-nxn-adjacency-matrix-how-can-one-compute-the-number-of-triangles-in-th">Given an nxn Adjacency matrix, how can one compute the number of triangles in the graph (Matlab)?</a>)</p>
  
  <p>Given my matrix <code>A</code>, <code>numpy.matmul()</code> computes the cube of <code>A</code> without problem, but for a large matrix <code>numpy.trace()</code> returns a negative number.</p>
  
  <p>I extracted the diagonal using <code>numpy.diagonal()</code> and summed the entries using <code>math.sum()</code> and also using a for loop -- both returned the same negative number as <code>numpy.trace()</code>.</p>
  
  <p>An attempt with <code>math.fsum()</code> finally returned (the assumably correct) number 4,088,103,618 -- a seemingly small number for both python and for my 64-bit operating system, especially since python documents claim integer values are unlimited.</p>
  
  <p>Surely this is an overflow or undefined behavior issue, but where does the inconsistency come from? I have performed the test on the following post to successfully validate my system architecture as 64 bit, and therefore numpy should also be a 64 bit package.
  <a href="https://stackoverflow.com/questions/33553549/do-i-have-numpy-32-bit-or-64-bit">Do I have Numpy 32 bit or 64 bit?</a></p>
  
  <p>To visualize the summation process print statements were added to the for-loop, output appears as follows with an asterisk marking the interesting line.</p>
  
  <pre><code>.
  .
  .
  adding diag val 2013124 to the running total 2140898426 = 2142911550
  adding diag val 2043358 to the running total 2142911550 = 2144954908
  adding diag val 2035410 to the running total 2144954908 = 2146990318
  adding diag val 2000416 to the running total 2146990318 = -2145976562 *
  adding diag val 2062276 to the running total -2145976562 = -2143914286
  adding diag val 2092890 to the running total -2143914286 = -2141821396
  adding diag val 2092854 to the running total -2141821396 = -2139728542
  .
  .
  .
  </code></pre>
  
  <p>Why would adding 2000416 to 2146990318 create an overflow? The sum is only 2148990734 -- a very small number for python!</p>