3

Generally, python libraries provide flexibility to play with the number of bits for decimals. I want something similar to quantizer in MATLAB where I can fix total number of bits and number of bits for decimals.

Example: Let's have 8 bit as total no. of bits. Let's fix 6 bits for decimal. There will 1 bit for sign. I will have 1 bit for integer.

So now given 255, the library should be able to quantize it to 1.984375.

I am not able to find any library for this purpose.

Thanks in Advance.

  • I don't know of a library that does that—and Stack Overflow is not a good place to ask for library recommendations. You may be interested in [this blog post](http://stupidpythonideas.blogspot.com/2015/01/ieee-floats-and-python.html), where I show how to build something to interpret bits as floats with `bitstring`, but if you're looking to use it for serious computation it'll be way too slow (I suspect PyPy would be a lot faster than CPython, but still not fast enough…), and it's obviously not a library you can just use off the rack. – abarnert Apr 03 '18 at 05:57
  • Although, if I interpret "integer" and "decimal" literally, it sounds like you're looking for fixed point, not floating point, and you want it in decimal rather than binary? (But what does 1 bit of decimal integer get you? That's not even a whole digit…) – abarnert Apr 03 '18 at 05:59
  • I don't know if such a library exists but assuming from your example a sign bit of `1` means a positive float, this could be a way to 'quantize' it `(255>>7 and 1 or -1)*((255>>6 & 2**1-1) + (255>>2)/2**6)` (example for 255). – eugenhu Apr 03 '18 at 06:26
  • Also if you can perhaps provide some example matlab code that does what you're trying to do. – eugenhu Apr 03 '18 at 06:29
  • Possible duplicate of [Fixed-point arithmetic](https://stackoverflow.com/questions/422247/fixed-point-arithmetic) – maxymoo Apr 03 '18 at 06:49
  • maxymoo , i have seen that post and tried all the libraries mentioned in the post. It doesnot work – user3572719 Apr 03 '18 at 16:23

0 Answers0