def test_declare(n): """ >>> test_declare(100) (100, 100) >>> test_declare(100.5) (100, 100) """ x = cython.declare(cython.int) y = cython.declare(cython.int, n) if cython.compiled: cython.declare(xx=cython.int, yy=cython.long) i = cython.sizeof(xx) ptr = cython.declare(cython.p_int, cython.address(y)) return y, ptr[0]
def test_sizeof(): """ >>> test_sizeof() True True True True True """ x = cython.declare(cython.bint) print(cython.sizeof(x) == cython.sizeof(cython.bint)) sizeof = cython.sizeof print( sizeof(cython.char) <= sizeof(cython.short) <= sizeof(cython.int) <= sizeof(cython.long) <= sizeof(cython.longlong)) print(cython.sizeof(cython.uint) == cython.sizeof(cython.int)) print(cython.sizeof(cython.p_int) == cython.sizeof(cython.p_double)) if cython.compiled: print(cython.sizeof(cython.char) < cython.sizeof(cython.longlong)) else: print(cython.sizeof(cython.char) == 1)
def test_declare(n): """ >>> test_declare(100) (100, 100) >>> test_declare(100.5) (100, 100) >>> test_declare(None) #doctest: +ELLIPSIS Traceback (most recent call last): ... TypeError: ... """ x = cython.declare(cython.int) y = cython.declare(cython.int, n) if cython.compiled: cython.declare(xx=cython.int, yy=cython.long) i = cython.sizeof(xx) ptr = cython.declare(cython.p_int, cython.address(y)) return y, ptr[0]
def cast_number(value: str): """ Convert numbers as string to an int or float """ # optimises for the common case of integers that fit into a C long intval: cython.long = 0 safe_max_long: cython.long = 2**(cython.sizeof(cython.long) * 8 - 2) too_large = safe_max_long + 10 for ch in value: if ch in '.eE': return float(value) elif ch in '0123456789': if intval >= safe_max_long // 10: intval = too_large else: intval = intval * 10 + (ord(ch) - ord('0')) else: intval = too_large if intval < too_large: return intval return int(value)
def test_sizeof(): """ >>> test_sizeof() True True True True True """ x = cython.declare(cython.bint) print(cython.sizeof(x) == cython.sizeof(cython.bint)) print(cython.sizeof(cython.char) <= cython.sizeof(cython.short) <= cython.sizeof(cython.int) <= cython.sizeof(cython.long) <= cython.sizeof(cython.longlong)) print(cython.sizeof(cython.uint) == cython.sizeof(cython.int)) print(cython.sizeof(cython.p_int) == cython.sizeof(cython.p_double)) if cython.compiled: print(cython.sizeof(cython.char) < cython.sizeof(cython.longlong)) else: print(cython.sizeof(cython.char) == 1)
# -*- coding: utf-8 -*- # @Author: lcl1026504480 # @Date: 2019-08-29 17:11:37 # @Last Modified by: lcl1026504480 # @Last Modified time: 2019-08-29 17:17:06 import cython cython.declare(x=cython.int, x_ptr=cython.p_int) x = 5 x_ptr = cython.address(x) cython.declare(n=cython.longlong) print(cython.sizeof(cython.longlong)) n = 400 print(cython.sizeof(n)) MyStruct = cython.struct(x=cython.int, y=cython.int, data=cython.double) a = cython.declare(MyStruct) T = cython.typedef(cython.p_int) # ctypedef int* T # t1 = cython.cast(T, t) # t2 = cython.cast(T, t, typecheck=True)