Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed-point arithmetic

Tags:

python

math

Does anyone know of a library to do fixed point arithmetic in Python? Or, does anyone has sample code?

like image 210
Kurt Pattyn Avatar asked Jan 07 '09 21:01

Kurt Pattyn


2 Answers

Another option worth considering if you want to simulate the behaviour of binary fixed-point numbers beyond simple arithmetic operations, is the spfpm module. That will allow you to calculate square-roots, powers, logarithms and trigonometric functions using fixed numbers of bits. It's a pure-python module, so doesn't offer the ultimate performance but can do hundreds of thousands of arithmetic operations per second on 256-bit numbers.

like image 99
rwp Avatar answered Sep 29 '22 11:09

rwp


recently I'm working on similar project, https://numfi.readthedocs.io/en/latest/

>>> from numfi import numfi  
>>> x = numfi(0.68751,1,6,3)
>>> x + 1/3
numfi([1.125]) s7/3-r/s
>>> np.sin(x)
numfi([0.625     ]) s6/3-r/s
like image 23
ZinGer_KyoN Avatar answered Sep 29 '22 13:09

ZinGer_KyoN