Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange type promotion in arithmetic operation

Tags:

python

cython

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.

like image 788
MarcinBurz Avatar asked Nov 10 '22 11:11

MarcinBurz


1 Answers

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.

like image 122
ecatmur Avatar answered Nov 14 '22 23:11

ecatmur