[SciPy-user] Arithmetic Errors

Travis Oliphant oliphant.travis at ieee.org
Mon Jul 17 17:30:09 EDT 2006


John Lawless wrote:
>      How do I instruct scipy to give me an error, or better yet compute
> the right answer, as opposed to silently ignoring an integer overflow? 
> Two examples are:
>
>   
>>>> from scipy import *
>>>> array((3000))*array((1000000))
>>>>         
> -1294967296
>   
>>>> sum(3000*ones(1000000))
>>>>         
> -1294967296
>
>   
You have to use object arrays. 

array(3000,'O')*array(1000000,'O')

or use array scalars with the error set as over='raise'

seterr(over='raise')
int32(3000)*int32(1000000)


For arrays, we don't check for over-flow as this is a time-consuming 
procedure that slows down all the calculations.   There is a dtype= 
keyword argument to the sum method of arrays which allows you to change 
the data-type over which the sum proceeds.   This could also be used to 
obtain the result:

3000*ones(1000000).sum(dtype=int64)



-Travis




More information about the SciPy-User mailing list