Fixed-point arithmetic

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


Asked by: Freddie540 | Posted: 24-09-2021






Answer 1

If you are interested in doing fixed point arithmetic, the Python Standard Library has a decimal module that can do it.

Actually, it has a more flexible floating point ability than the built-in too. By flexible I mean that it:

  • Has "signals" for various exceptional conditions (these can be set to do a variety of things on signaling)

  • Has positive and negative infinities, as well as NaN (not a number)

  • Can differentiate between positive and negative 0

  • Allows you to set different rounding schemes.

  • Allows you to set your own min and max values.

All in all, it is handy for a million household uses.

Answered by: Walter704 | Posted: 25-10-2021



Answer 2

The deModel package sounds like what you're looking for.

Answered by: William828 | Posted: 25-10-2021



Answer 3

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.

Answered by: Rubie493 | Posted: 25-10-2021



Answer 4

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

Answered by: Rafael846 | Posted: 25-10-2021



Similar questions

Fixed-Point binary unpacking in python

I am struggling with a number-format problem in Python 3.6. My goal is to convert binary data from a file into printable decimal numbers. As an example, I need to convert two little-endian bytes in the form of a byte string... b'\x12\00' into its big-endian binary form... 0000000000010010 and finally to its


python - How can I create a fixed-point filter of type uint8 or int16 using fdatool in MATLAB?

I need to get the coefficients of a filter as uint8 or int16. I use fdatool to create a HPF. When I choose the fixed-point option and export the coefficients they are still floating point. Also, how can I choose the number of coefficients?


Regarding getting the decimal values as a fixed-point number in python

I have some decimal values as: 0 11.586 11.915 11.034 0 4.896 9.675 13.564 0 8.676 1.275 17.034 0 18.424 15.115 13.316 0 1.174 7.515 21.296 0 24.054 1.855 17.746 0 0.164 21.775 2.776 But i need the values in neat format and like this: 0 11.586 11.915 11.034 0 04.896 09.675 13.564 0 08.676 0...


python - How to implement fixed-point binary support in numpy

I have a homebrew binary fixed-point arithmetic support library and would like to add numpy array support. Specifically I would like to be able to pass around 2D arrays of fixed-point binary numbers and do various operations on them such as addition, subtraction, multiplication, rounding, changing of fixed point format, etc. The fixed-point support under the hood works on integers, and separate tracking of fixed-po...


python - Why is it not possible to implement fixed-point combinator like in the definition?

I'm trying to implement the Y-combinator like in the definition by Curry. This code does not work. It causes infinite recursion. F = (lambda f: (lambda x: (1 if x == 0 else (x * (f(x-1)))))) Y = ( lambda f: (lambda x: f(x(x))) (lambda x: f(x(x))) ) Y(F)(3) However, this on...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top