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

标签 python cython

为什么这个 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/

相关文章:

python - 我应该将我的 cython 文件放在 python 发行版中的什么位置?

python - 通过 Cython(回调)将 Python 函数应用于 std::vector

python - Cython 中的 C++ 指针

python - 同时进行线程和初始化(它是如何工作的?)

python - Cython 的 prange 没有提高性能

python - 我严重破坏了 Cython,它的性能比纯 Python 还差。为什么?

Python:SSLError、握手错误、意外的 EOF

python - 如何在文本元素后附加 <br> 标签?

python - 在 Windows Vista 上安装 Python SSL 模块

python - 使用 strptime 的日期格式不正确