gpt4 book ai didi

python - 算术运算中的奇怪类型提升

转载 作者:太空狗 更新时间:2023-10-30 01:33:38 30 4
gpt4 key购买 nike

为什么这个 cython 函数:

cimport numpy as np
cimport cython

def foo(np.uint32_t b):
cdef np.int32_t a = 0


if a-b <0: return 0
else: return 1

对于 foo(1) 返回 1?我在 C 中编译了类似的代码,但没有观察到两个操作数 (a, b) 都已提升为 unsigned int。

最佳答案

1 是正确的结果;有符号操作数应转换为相应的无符号类型。

6.3.1.8 Usual arithmetic conversions

[...]
- Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

关于python - 算术运算中的奇怪类型提升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30869776/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com