Strange type promotion in arithmetic operation

167 views Asked by At

Why this cython function:

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, for foo(1)? I compiled similar code in C, and didn't observe both operands (a, b) had been promoted to unsigned int.

1

There are 1 answers

3
ecatmur On BEST ANSWER

1 is the correct result; the signed operand should be converted to the corresponding unsigned type.

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.