12

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

Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
Kurt Pattyn
  • 2,758
  • 2
  • 30
  • 42

4 Answers4

17

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.

John Mulder
  • 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
7

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

Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
geschema
  • 2,464
  • 4
  • 30
  • 41
4

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.

rwp
  • 1,786
  • 2
  • 17
  • 28
2

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
  • 1
    How many more questions will you paste the same thing? – gurkan May 21 '21 at 09:39
  • 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
  • @gurkan Understood, I will delete other answer and keep this one – ZinGer_KyoN May 24 '21 at 01:47
  • 1
    This 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