2014-11-05 27 views
2

numpy的版本1.9.0numpy的位運算錯誤

1 & (2**63) 
0 

np.bitwise_and(1, 2**63) 
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' 

np.bitwise_and(1, 2**63 + 100) 
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' 

np.bitwise_and(1, 2**64) 
0 

這是bug還是我失去了一些東西?

回答

6

轉換爲uint64第一:

np.bitwise_and(np.uint64(1), np.uint64(2**63)) 

這裏是檢查規則轉換蟒蛇整數numpy的整數代碼:

print np.array([2**30]).dtype 
print np.array([2**31]).dtype 
print np.array([2**63]).dtype 
print np.array([2**64]).dtype 

輸出:

int32 
int64 
uint64 
object 

我想np.bitwise_and(1, 2**63)由於2**63超出範圍而引發錯誤int32和int64。

np.bitwise_and(1, 2**64)工作,因爲它會使用Python的長對象。

我們需要閱讀源代碼以瞭解詳細信息。

+0

它的工作原理。你認爲這是一個錯誤還是有一個原因,在這裏需要手動轉換?請注意,2 ** 64沒有任何問題。 – jf328 2014-11-05 13:20:27

+0

我更新了答案,我認爲這不是一個錯誤。 – HYRY 2014-11-05 13:47:46