hbutils.binary.float
Binary floating-point type utilities for structured I/O.
This module provides floating-point type definitions for binary input/output
operations based on the struct module. It defines convenience classes and
instances for standard IEEE-754 floating-point sizes (16-bit, 32-bit, 64-bit),
along with aliases matching the conventional C type names.
The module exposes the following public components:
CFloatType- Float type wrapper with binary read/write helpersc_float16- 16-bit (half precision) float type instancec_float32- 32-bit (single precision) float type instancec_float64- 64-bit (double precision) float type instance
Example:
>>> import io
>>> from hbutils.binary.float import c_float32
>>>
>>> buffer = io.BytesIO()
>>> c_float32.write(buffer, 3.5)
>>> buffer.seek(0)
0
>>> c_float32.read(buffer)
3.5
Note
These types are thin wrappers around hbutils.binary.base.CMarkedType,
which uses the struct module for binary packing and unpacking.
__all__
- hbutils.binary.float.__all__ = ['CFloatType', 'c_float16', 'c_float32', 'c_float64', 'c_float', 'c_double']
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
c_float16
- hbutils.binary.float.c_float16 = <hbutils.binary.float.CFloatType object>
Reading and writing half-precision (16-bit) floating-point numbers.
This type uses 2 bytes to represent floating-point values with reduced precision.
Example:
>>> import io >>> from hbutils.binary.float import c_float16 >>> >>> buffer = io.BytesIO() >>> c_float16.write(buffer, 1.5) >>> buffer.seek(0) 0 >>> c_float16.read(buffer) 1.5
c_float32
- hbutils.binary.float.c_float32 = <hbutils.binary.float.CFloatType object>
Reading and writing single-precision (32-bit) 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-bit) 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"
c_float
c_double
CFloatType
- class hbutils.binary.float.CFloatType(mark: str, size: int)[source]
Float type class for binary I/O operations, based on the
structmodule.This class extends
hbutils.binary.base.CMarkedTypeto provide specialized handling for floating-point numbers in binary format. It enforces conversion tofloatwhen writing values.Example:
>>> import io >>> from hbutils.binary.float import CFloatType >>> >>> float_type = CFloatType('f', 4) >>> buffer = io.BytesIO() >>> float_type.write(buffer, 1.25) >>> buffer.seek(0) 0 >>> float_type.read(buffer) 1.25