I want to do some simulation of C language calculation in Python. For example, unsigned short, single precision float ...
ushort(0xffff) + 1 -> 0
0.1f * 0.1f -> ...
Are there some library to do this in Python?
I can use ctypes to create unsigned short, single float, but they cann't do math operation:
a = c_uint16(0xffff)
b = c_uint16(0x01)
a+b -> TypeError
Or, I can use numpy:
>>> np.uint16(0xffff) + np.uint16(0x01)
Warning: overflow encountered in ushort_scalars
0
but it's very slow comparing to Python's normal calculation:
>>> timeit.timeit("a+b", "import numpy as np;a=np.uint16(0xfffe);b=np.uint16(0x01)")
0.35577465681618037
>>> timeit.timeit("0xfffe+0x01")
0.022638104432360251
>>> timeit.timeit("np.uint16(0xfffe) + np.uint16(0x01)", "import numpy as np")
5.904765399236851
Edit:
>>> timeit.timeit("a+b", "a=0xfffe;b=0x01")
0.040062221014295574
def add(a,b): return (a+b) % 65536?