[Numpy-discussion] Silent overflow of Int32 array

Tim Churches tchur at optushome.com.au
Sat Apr 9 17:25:43 EDT 2005


I just got caught by code equivalent to this (with NumPy 23.8 on 32 bit 
Linux):

 >>> import Numeric as N
 >>> a = N.array((2000000000,1000000000),typecode=N.Int32)
 >>> N.add.reduce(a)
-1294967296

OK, it is an elementary mistake, but the silent overflow caught me 
unawares. casting the array to Float64 before summing it avoids the  
error, but in my instance the actual data is a rank-1 array of 21 
million integers with a mean value of about 140 (which adds up more than 
sys.maxint), and casting to Float64 will use quite a lot of memory (as 
well as taking some time).

Any advice for catching or avoiding such overflow without always 
incurring a performance and memory hit by always casting to Float64? 
Shouldn't add.reduce() be checking for overflow and raising an error? 
Then it would be easy to upcast only when overflow (or underflow) 
occurs, rather than always.

Tim C





More information about the NumPy-Discussion mailing list