2015-08-14 208 views
9

大多數使用python,我已經被寵壞了,不必擔心整數溢出。現在我正在使用numpy,我不得不再次擔心它。我想numpy在溢出的情況下出錯,但它似乎不適用於int64。得到numpy警告整數溢出

import numpy 
numpy.seterr(all='raise') 
print("{:,}".format(numpy.prod([10]*50))) 
# -5,376,172,055,173,529,600 
print("{:,}".format(numpy.int64(3200000000) * numpy.int64(3200000000))) 
# -8,206,744,073,709,551,616 
print("{:,}".format(numpy.int32(320000) * numpy.int32(320000))) 
# FloatingPointError: overflow encountered in int_scalars -- Finally getting an error! 

我可以隨時添加dtype=object來解決這些問題,但我認爲Int64的不夠好大部分時間,它是太可怕了,它可以在這個硬故障檢測方法。

爲什麼seterr只能用於int32?我可以使它適用於int64嗎?

numpy.seterr文檔,我可以找到這可能暗示,這可能是爲什麼的情況下的唯一部分是下列短文:

注意整數標量操作類型(如INT16)是 處理像浮點,並受這些設置的影響。

data type文檔中沒有提示int32和int64在概念上有所不同。不確定int64是否被認爲是「整數標量類型」。

+3

看起來像一個numpy的bug;請參閱https://github.com/numpy/numpy/pull/3199。奇怪的是,打印(numpy.int64(2 ** 63-1)* numpy.int64(2))*會在我的系統上引發異常(Ubuntu 14.04,Python 2.7.6,numpy 1.8.2),但是您的示例顯示了小車行爲。 –

回答

2

事實上,行爲似乎取決於int類型的大小。這是一個包含你的案例的列表,並增加了一些(已設置numpy.seterr(all='raise'))。

In [25]: numpy.int(3200000000) * numpy.int(3200000000) 
Out[25]: 10240000000000000000 

In [26]: numpy.int8(3200000000) * numpy.int8(3200000000) 
Out[26]: 0 

In [27]: numpy.int16(3200000000) * numpy.int16(3200000000) 
--------------------------------------------------------------------------- 
FloatingPointError      Traceback (most recent call last) 
<ipython-input-27-a6185c9da0fd> in <module>() 
----> 1 numpy.int16(3200000000) * numpy.int16(3200000000) 

FloatingPointError: overflow encountered in short_scalars 

In [28]: numpy.int32(3200000000) * numpy.int32(3200000000) 
--------------------------------------------------------------------------- 
FloatingPointError      Traceback (most recent call last) 
<ipython-input-28-a3909399b44a> in <module>() 
----> 1 numpy.int32(3200000000) * numpy.int32(3200000000) 

FloatingPointError: overflow encountered in int_scalars 

In [29]: numpy.int64(3200000000) * numpy.int64(3200000000) 
Out[29]: -8206744073709551616