ieee754 – Handle IEEE754 floating-point format
Handle IEEE754 floating-point format.
The library.ieee754 module reads float numbers in a IEEE754
representation. This module do not implement the full specification of
the standard and only read IEEE754 representation. The implementation
supports the following subset:
32 bits - simple (binary32)
64 bits - double (binary64)
Using ieee754
The simple examples below shows how converting a IEEE754 representation of
a float number expressed as a binary string in a float object.
We can read simple precision float number as shown below.
>>> from library.ieee754 import IEEE754
>>> IEEE754(b"\xab\xaa\xaa\x3e").value
0.3333333432674408
>>> IEEE754(b"\xab\xaa\xaa\x3e").value
0.3333333432674408
>>> IEEE754(b"\x00\x00\x00\x00").value
0.0
>>> IEEE754(b"\x00\x00\x80\x7f").value
inf
And the same with double precision.
>>> IEEE754( b"\x00\x00\x00\x00\x00\x00\xf0\x3f").value
1.0
>>> IEEE754(b"\x00\x00\x00\x00\x00\x00\xf0\xff").value
-inf
>>> IEEE754(b"\x55\x55\x55\x55\x55\x55\xd5\x3f").value
0.3333333333333333
The module supports only simple and double precision based on the lenght
of the binary string; Any value other than 4 bytes (32 bits) or 8 bytes
(64 bits) raise an KeyError Exception. The example below have a 56 bits
lenght binary string.
>>> IEEE754(b"\x55\x55\x55\x55\x55\x55\xd5").value
KeyError: 'Unsupported IEEE754 Precision (56 bits)'
Reference
- class darkbridge.library.ieee754.IEEE754(buffer: bytes)
Bases:
objectIEEE754 float number.
The function supports single precision (32 bits), double precision (64 bits).
- Parameters:
buffer – Bytes object of IEEE754 representation of the float number with the most significant byte is at the end of the byte array (
byteorder = little).- Raises:
KeyError – Unsupported IEEE754 Precision.
- _get_value(part: str) int
Return the value of part.
This method extract the value from the IEEE representation and return it as an integer value.
- Parameters:
part – name of the part (“sign”, “exponent”, “fraction”). Another name raise the
KeyErrorexception.- Returns:
Value of the part as an integer