[Numpy-discussion] Can numpy catch this error for me?
Robert Kern
robert.kern@gmail....
Tue Apr 7 15:03:45 CDT 2009
On Tue, Apr 7, 2009 at 14:19, Nathaniel Peterson
<nathanielpeterson08@gmail.com> wrote:
>
> 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?
No, sorry.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion
mailing list