In [1]:
%load_ext cython
In [2]:
%%cython
def f1():
# 2 bytes
cdef short x = 1
return x
def f2():
# 4 bytes
cdef int x = 1
return x
def f3():
# 4 or 8 bytes, depends on your OS
cdef long x = 1
return x
def f4():
# 8 bytes
cdef long long x = 1
return x
In [3]:
print(type(f1()))
print(type(f2()))
print(type(f3()))
print(type(f4()))
In [4]:
# Biggest unsigned short, 2 bytes => 16 bits
print(2**16 - 1)
# Biggest unsigned int, 4 bytes => 32 bits
print(2**32 - 1)
# Biggest unsigned long long, 8 bytes => 64 bits
print(2**64 - 1)
Python can handle very big numbers. Be careful passing big numbers into Cython functions.
In [5]:
%%cython
cpdef can_handle_ushort(unsigned short x):
return x
cpdef can_handle_uint(unsigned int x):
return x
cpdef can_handle_ulong_long(unsigned long long x):
return x
In [6]:
can_handle_ushort(2**16-1)
Out[6]:
In [7]:
can_handle_ushort(2**16)
In [8]:
can_handle_uint(2**32-1)
Out[8]:
In [9]:
can_handle_uint(2**32)
In [10]:
can_handle_ulong_long(2**64-1)
Out[10]:
In [11]:
can_handle_ulong_long(2**64)
In [ ]: