Floating and Fixed Point
Binary and decimal floating point, fixed point, Q format, and BCD
Binary floating point
| Type | Format | Size |
|---|---|---|
Float8E4M3 | OCP 8-bit E4M3 (no infinity, max 448) | 1 byte |
Float8E5M2 | OCP 8-bit E5M2 (max 57344) | 1 byte |
Float16 | IEEE 754 binary16 | 2 bytes |
BFloat16 | bfloat16 | 2 bytes |
Float32 | IEEE 754 binary32 | 4 bytes |
Float64 | IEEE 754 binary64 | 8 bytes |
Float80 | x87 80-bit extended precision | 10 bytes |
Float128 | IEEE 754 binary128 | 16 bytes |
The Python value is a float. Encoding rounds to the nearest representable value (ties to even). A finite value beyond the format’s largest finite value is a ValidationError, as is an infinity in a format without one (E4M3). Float80 and Float128 are wider than a Python float: every float encodes exactly, and decoding rounds to the nearest float, overflowing to infinity. Each type is an alias of Float(format, byte_order=None); the byte order overrides the model’s for that field (the 8-bit formats have none).
from binal import BFloat16, BinaryModel, Float8E4M3, Float80
class Weights(BinaryModel, byte_order="little"):
scale: BFloat16
quantized: Float8E4M3
accumulator: Float80
Weights(scale=1.0, quantized=448.0, accumulator=0.1).encode() Decimal floating point
| Type | Format | Size |
|---|---|---|
Decimal32BID, Decimal32DPD | IEEE 754 decimal32 | 4 bytes |
Decimal64BID, Decimal64DPD | IEEE 754 decimal64 | 8 bytes |
Decimal128BID, Decimal128DPD | IEEE 754 decimal128 | 16 bytes |
The Python value is a decimal.Decimal (an int is accepted too; a float is rejected, since it is not decimal). BID stores the significand as a binary integer; DPD stores it as densely packed decimal. Encoding is exact: a value with more digits than the format’s precision (7, 16, or 34), or an exponent the format cannot reach by adding or removing trailing zeros, raises EncodeError instead of being rounded. Infinities, NaNs with a payload, and signaling NaNs round-trip. Decoding keeps the cohort (Decimal("1.0") and Decimal("1.00") are distinct). Each type is an alias of DecimalFloat(bits, encoding).
from decimal import Decimal
from binal import BinaryModel, Decimal64DPD
class Price(BinaryModel, byte_order="big"):
amount: Decimal64DPD
Price(amount=Decimal("-7.50")).encode() # b'\xa2\x30\x00\x00\x00\x00\x03\xd0' Fixed point
FixedPoint(bits, fraction_bits, signed=True) stores a float as a bits-wide integer scaled by 2**fraction_bits. Encoding rounds to the nearest step (ties to even); a value whose scaled integer does not fit is a ValidationError. Q(m, n) is the Q-format spelling: Q(15, 16) is a signed 32-bit value with 16 fraction bits, and Q(m, n, signed=False) is the unsigned UQm.n (m + n bits).
DecimalFixed(bits, scale, signed=True) stores a Decimal as a bits-wide integer scaled by 10**scale, so DecimalFixed(32, 2) holds amounts in cents. The value must be exact at that scale: Decimal("1.234") with scale=2 is an error, not rounded.
from decimal import Decimal
from typing import Annotated
from binal import BinaryModel, DecimalFixed, FixedPoint, Q
class Sensor(BinaryModel, byte_order="big"):
temperature: Annotated[float, FixedPoint(16, 8)]
gain: Annotated[float, Q(1, 14)]
balance: Annotated[Decimal, DecimalFixed(32, 2)]
Sensor(temperature=21.5, gain=0.75, balance=Decimal("-12.34")).encode() Binary-coded decimal
BCD(digits, packed=True, signed=False) stores an int as decimal digits. Packed BCD holds two digits per byte, with a leading zero nibble when the count is odd; unpacked BCD holds one digit per byte, in the low nibble. A signed BCD adds a sign nibble — 0xC for plus and 0xD for minus — as the last nibble when packed, and as the high nibble of the last byte when unpacked; decoding also accepts 0xA, 0xE, 0xF (plus) and 0xB (minus). An invalid digit or sign nibble is a DecodeError.
from typing import Annotated
from binal import BCD, BinaryModel
class Record(BinaryModel, byte_order="big"):
year: Annotated[int, BCD(4)]
balance: Annotated[int, BCD(5, signed=True)]
code: Annotated[int, BCD(3, packed=False)]
Record(year=2024, balance=-12345, code=42).encode()
# b'\x20\x24' + b'\x12\x34\x5d' + b'\x00\x04\x02'