算术运算中的奇怪类型提升
Strange type promotion in arithmetic operation
为什么这个 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
returns 1,对于 foo(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.
为什么这个 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
returns 1,对于 foo(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.