I have some code like:
import math, csv, sys, re, time, datetime, pickle, os, gzip
from numpy import *
x = [1, 2, 3, ... ]
y = sum(x)
The sum of the actual values in x is 2165496761, which is larger than the limit of 32bit integer. The reported y value is -2129470535, implying integer overflow.
Why did this happen? I thought the built-in sum was supposed to use Python’s arbitrary-size integers?
See How to restore a builtin that I overwrote by accident? if you’ve accidentally done something like this at the REPL (interpreter prompt).
The reason why you get this invalid value is that you’re using
np.sumon aint32. Nothing prevents you from not using anp.int32but anp.int64ornp.int128dtypeto represent your data. You could for example just useOn a side note, please make sure that you never use
from numpy import *. It’s a terrible practice and a habit you must get rid of as soon as possible. When you use thefrom ... import *, you might be overwriting some Python built-ins which makes it very difficult to debug. Typical example, your overwriting of functions likesumormax…