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, ...)
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, ...)
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)
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
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.
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
.
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
The development version of gmpy2
supports the 96-bit IEEE numeric type.
>>> import gmpy2
>>> gmpy2.version()
'2.1.0a1'
>>> gmpy2.set_context(gmpy2.ieee(96))
>>> gmpy2.get_context()
context(precision=83, real_prec=Default, imag_prec=Default,
round=RoundToNearest, real_round=Default, imag_round=Default,
emax=4096, emin=-4175,
subnormalize=True,
trap_underflow=False, underflow=False,
trap_overflow=False, overflow=False,
trap_inexact=False, inexact=False,
trap_invalid=False, invalid=False,
trap_erange=False, erange=False,
trap_divzero=False, divzero=False,
allow_complex=False,
rational_division=False)
>>> gmpy2.mpfr(1)/7
mpfr('0.14285714285714285714285714',83)
>>>
It is also possible in older versions of gmpy2
but requires a bit more effort.
>>> import gmpy2
>>> gmpy2.version()
'2.0.8'
>>> ieee96 = gmpy2.context(precision=83, emax=4096, emin=-4175, subnormalize=True)
>>> gmpy2.set_context(ieee96)
>>> gmpy2.mpfr(1)/7
mpfr('0.14285714285714285714285714',83)
>>>
You may need to down to download the source directly from https://github.com/aleaxit/gmpy . Some very early wheels are available at https://pypi.python.org/pypi/gmpy2/2.1.0a1 .
Disclaimer: I maintain gmpy2
.