NumPy位运算错误

3

NumPy版本为1.9.0

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 吗?还是我漏掉了什么?
1个回答

7

首先需要转换为uint64

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

以下是检查将Python整数转换为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的long对象。

我们需要阅读源代码才能了解细节。


它可以工作。你认为这是一个错误还是手动转换是必要的?请注意,2 ** 64没有任何问题。 - jf328

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接