Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a number to 12 bits precision in python?

I need to store a float variable with 12 bits precision in Python

I know that to convert a variable in float there is a float function but how can I specify the size of the float in bits? e.g. (12, 16, ...)

like image 540
StarBucK Avatar asked Oct 17 '25 18:10

StarBucK


2 Answers

As mentioned in other answers, this doesn't really exist in pure python data types, see the docs

However, you can use numpy to specify explicit data types e.g.

  • numpy.float16
  • numpy.float32
  • numpy.float64

You can also use extended precision of numpy.float96 which seems to be what you are after as 12 bytes is 96 bits, for example

import numpy as np
high_prec_array = np.array([1,2,3], dtype=np.float96)

Caveats

As pointed out in comments and links, this isn't true 12 byte accuracy. Rather, 80 bit (10 byte) padded by 2 zero bytes. This may be sufficient if you just care about compatibility.

This precision may not be available on all platforms

In the tables below, platform? means that the type may not be available on all platforms. Compatibility with different C or Python types is indicated: two types are compatible if their data is of the same size and interpreted in the same way.

Also read this about the caveats of using such exotic types

  • https://stackoverflow.com/a/17023995/4013571
  • https://mail.scipy.org/pipermail/scipy-dev/2008-March/008562.html
  • https://www.reddit.com/r/learnpython/comments/3l7f3v/a_sneaky_numpy_feature_for_anyone_interested_in/
  • https://stackoverflow.com/a/18537604/4013571

I found this quite illuminating. I would conclude that if you want to absolutely guarantee 96bit precision then python is not the correct choice as the inherent ambiguity in the available extended precision comes from the ambiguity in your C distribution. Given your physics background I would suggest using Fortran if you want to guarantee stability.

Define your own type in C++

For the interested, advanced user, it may be possible to define your own data type. The numpy guide on user defined types states

As an example of what I consider a useful application of the ability to add data-types is the possibility of adding a data-type of arbitrary precision floats to NumPy.

You can therefore try using boost/multiprecision/cpp_bin_float.hpp if you fervently wish to keep your code in python.

like image 74
Alexander McFarlane Avatar answered Oct 20 '25 07:10

Alexander McFarlane


The float type in python is fixed. Often 64 bits, but it is implementation-dependent.

You can use sys.float_info to know the size of floats, but you are not supposed to be able to change it.

https://docs.python.org/3/library/sys.html#sys.float_info

EDIT:

If you really need to specify the float size, you can rely on external libraries, such as numpy. See the very informative answer of Alexander McFarlane for lots of details

like image 29
Pac0 Avatar answered Oct 20 '25 07:10

Pac0