Does anyone know of a library to do fixed point arithmetic in Python? Or, does anyone has sample code?
-
I think he means fixed point. I have to ask why? Because if its for performance, you won't get any. – Pyrolistical Jan 07 '09 at 21:58
-
2I have to write an emulation library for algorithm calculations to be implemented in an FPGA (so, no FP support) – Kurt Pattyn Jan 08 '09 at 18:45
4 Answers
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.

- 9,765
- 7
- 33
- 37
-
27?? The Decimal pkg lets you define the precision but it is base10 precision. You can state 6 digits to the right of the decimal point. For doing hardware modeling you want to define the number of bits, the precision is defined by the number of base2 digits to the right of the binary point. Curious, how the Decimal package was a good solution. – Christopher Felton Aug 25 '11 at 18:39
The deModel package sounds like what you're looking for.

- 31,947
- 10
- 111
- 111

- 2,464
- 4
- 30
- 41
-
Another fixed-point package is available [here](https://bitbucket.org/cfelton/fixed_point/overview) – Christopher Felton Aug 25 '11 at 18:28
-
2Is the numpy wrapper as fast as built in numpy types uses object type? – Mark Horvath Jun 04 '16 at 10:31
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.

- 1,786
- 2
- 17
- 28
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

- 89
- 5
-
1
-
2@gurkan I'm sorry if this is impropriety in community, but I'm not just "paste same thing". I developed a tool to solve a kind of problem, and provide my solution to these questioners. In each post I show how this work to different question with different example, I think this is a valid solution, at least an alternative solution – ZinGer_KyoN May 22 '21 at 02:43
-
Thanks for your help. However, you don't need to post the same answer for different questions. This reflects on you negatively rather than positively. It is enough to post your solution to the single question. It will already get reactions depend on the quality of your answer. – gurkan May 22 '21 at 09:19
-
-
1This was just my personal opinion. Because your similar answers came to the late answers section several times. No, if it didn't get a negative vote, of course you don't need to delete them – gurkan May 24 '21 at 08:40
-
@gurkan you're right at some point, after review these questions and answers I find that my answers are not 100% fit what questioners want. Some only want floating/fixed transforming, not fixed arithmetic, and some can be solve by simple integer calculation and function, no extra library is need. I will update my answers to better suit their need – ZinGer_KyoN May 25 '21 at 11:31