[Numpy-discussion] Can numpy catch this error for me?

Nathaniel Peterson nathanielpeterson08@gmail....
Tue Apr 7 14:19:24 CDT 2009


import numpy as np
import operator
np.seterr(all='raise')
a=np.arange(1)+1
print(a.dtype)
# int32
for num in range(1,17):
    a=np.arange(num)+1
    b=np.multiply.reduce(a)
    print('%s! = %s'%(num,b))
#     c=reduce(operator.mul,range(1,num+1))
#     assert(b==c)

The code above outputs

int32
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184

The results for 14! and above are wrong due to overflow of the int32 data
type.
Is there a way to setup numpy so it will raise an error when this occurs?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090407/b83c4849/attachment.html 


More information about the Numpy-discussion mailing list