Unusual integer behavior in ziton

I am having a weird problem with some whole manipulations in cython. When I compile this cython code ...

test.pyx

from libc.math cimport abs

cdef int c = 2047
cdef int b = 1009
print('%d'%(-(abs(b)&c)))

... like this

#!python
from subprocess import check_output

CFLAGS = check_output('python-config --cflags'.split()).decode('utf-8').strip()
LDFLAGS = check_output('python-config --ldflags'.split()).decode('utf-8').strip()

check_output('cython test.pyx'.split())

compile_call = ('clang -c test.c %s'%(CFLAGS)).split()
check_output(compile_call)

link_call = ('clang test.o -o test.so -shared %s'%(LDFLAGS)).split()
check_output(link_call)

import test

... I get 2130706744, but the correct answer -1009.

+4
source share
1 answer

Seems like this might be a bug in Cython. The corresponding line in test.cequals

__pyx_t_2 = __Pyx_PyInt_From_unsigned_int((-(__pyx_t_1 & __pyx_v_4test_c)));

Clearly interpreting our number as unsigned gives the wrong answer. However, if I delete the call abs, then I get

__pyx_t_1 = __Pyx_PyInt_From_int((-(__pyx_v_4test_b & __pyx_v_4test_c)));

and the result is correct. If I add cast to int as print('%d'%(-(<int>abs(b)&c))), then I will also get the correct answer.

Crossroads placed on Cython github page

: github:

, int , abs (-MAX_INT-1), . , , , , . , ...

+2

Source: https://habr.com/ru/post/1684332/


All Articles