hbutils.binary.float
This module provides floating-point type definitions for binary I/O operations.
It offers various precision levels of floating-point types (16-bit, 32-bit, 64-bit)
based on the struct module, with convenient read/write operations for binary files.
The module also provides aliases that map to the standard C float types.
CFloatType
- class hbutils.binary.float.CFloatType(mark: str, size: int)[source]
Float type class for binary I/O operations, based on
structmodule.This class extends CMarkedType to provide specialized handling for floating-point numbers in binary format.
- __init__(mark: str, size: int)
Constructor of
CMarkedType.- Parameters:
mark (str) – Format character for the struct module (e.g., ‘i’, ‘f’, ‘d’).
size (int) – Size of the type in bytes.
- property mark: str
Mark of the type.
The format character that will be used to read from and write to binary data with the
structmodule.- Returns:
The struct format character.
- Return type:
str
- read(file: BinaryIO) float[source]
Read a floating-point value from a binary file.
- Parameters:
file (BinaryIO) – The binary file to read from.
- Returns:
The floating-point value read from the file.
- Return type:
float
- property size: int
Size of the given type in bytes.
- Returns:
The size of the type.
- Return type:
int
c_float
c_double
c_float16
- hbutils.binary.float.c_float16 = <hbutils.binary.float.CFloatType object>
Reading and writing half-precision (16-bits) floating-point numbers.
This type uses 2 bytes to represent floating-point values with reduced precision.
c_float32
- hbutils.binary.float.c_float32 = <hbutils.binary.float.CFloatType object>
Reading and writing single-precision (32-bits) floating-point numbers.
This type uses 4 bytes to represent floating-point values.
- Examples::
>>> import io >>> import math >>> from hbutils.binary import c_float32 >>> >>> with io.BytesIO(b'\x00\x00\x90\x7f' ... b'\x00\x00\x80\x7f' ... b'\x00\xa0\x3e\xc1' ... b'\x00\x00\x70\x00') as file: ... print(c_float32.read(file)) ... print(c_float32.read(file)) ... print(c_float32.read(file)) ... print(c_float32.read(file)) nan inf -11.9140625 1.0285575569695016e-38 >>> with io.BytesIO() as file: ... c_float32.write(file, math.nan) ... c_float32.write(file, +math.inf) ... c_float32.write(file, -11.9140625) ... c_float32.write(file, 1.0285575569695016e-38) ... print(file.getvalue()) b'\x00\x00\xc0\x7f\x00\x00\x80\x7f\x00\xa0>\xc1\x00\x00p\x00'
c_float64
- hbutils.binary.float.c_float64 = <hbutils.binary.float.CFloatType object>
Reading and writing double-precision (64-bits) floating-point numbers.
This type uses 8 bytes to represent floating-point values with higher precision.
- Examples::
>>> import io >>> import math >>> from hbutils.binary import c_float64 >>> >>> with io.BytesIO(b'\x00\x00\x00\x00\x00\x00\xf8\x7f' ... b'\x00\x00\x00\x00\x00\x00\xf0\x7f' ... b'\x00\x00\x00\x00\x00\xd4\x27\xc0' ... b'\x00\x00\x00\x00\x00\x00\x0c8') as file: ... print(c_float64.read(file)) ... print(c_float64.read(file)) ... print(c_float64.read(file)) ... print(c_float64.read(file)) nan inf -11.9140625 1.0285575569695016e-38 >>> with io.BytesIO() as file: ... c_float64.write(file, math.nan) ... c_float64.write(file, +math.inf) ... c_float64.write(file, -11.9140625) ... c_float64.write(file, 1.0285575569695016e-38) ... print(file.getvalue()) b"\x00\x00\x00\x00\x00\x00\xf8\x7f\x00\x00\x00\x00\x00\x00\xf0\x7f\x00\x00\x00\x00\x00\xd4'\xc0\x00\x00\x00\x00\x00\x00\x0c8"