Skip to content

Repository files navigation

gfloat: Generic floating-point types in Python

An implementation of generic floating point encode/decode logic, handling various current and proposed floating point types:

The library favours readability and extensibility over speed (although the *_ndarray functions are reasonably fast for large arrays, see the benchmarking notebook). For other implementations of these datatypes more focused on speed see, for example, ml_dtypes, bitstring, MX PyTorch Emulation Library.

See https://gfloat.readthedocs.io for documentation, or dive into the notebooks to explore the formats.

For example, here's a table from the 02-value-stats notebook:

nameB: Bits in the formatP: Precision in bitsE: Exponent field width in bits0<x<11<x<InfminSubnormalmaxSubnormalminNormalmaxNormalExact in float16?Exact in float32?
p3109_k3p2sf321110.50.511.5TrueTrue
ocp_e2m1422150.50.516TrueTrue
p3109_k4p2sf422330.250.250.53TrueTrue
ocp_e2m36427230.1250.87517.5TrueTrue
ocp_e3m263311190.06250.18750.2528TrueTrue
p3109_k6p3sf63315150.031250.093750.12514TrueTrue
p3109_k6p4sf64215150.06250.43750.53.75TrueTrue
ocp_e4m384455702^-97/4*2^-70.015625448TrueTrue
ocp_e5m283559632^-163/2*2^-152^-1457344TrueTrue
p3109_k8p1se8176362n/an/a2^-632^62FalseTrue
p3109_k8p1ue818127125n/an/a2^-1272^125FalseTrue
p3109_k8p3se83563622^-173/2*2^-162^-1549152TrueTrue
p3109_k8p3sf83563632^-173/2*2^-162^-1557344TrueTrue
p3109_k8p3ue8361271252^-333/2*2^-322^-315/4*2^31FalseTrue
p3109_k8p3uf8361271262^-333/2*2^-322^-313/2*2^31FalseTrue
p3109_k8p4se84463622^-107/4*2^-80.0078125224TrueTrue
p3109_k8p4sf84463632^-107/4*2^-80.0078125240TrueTrue
p3109_k8p4ue8451271252^-187/4*2^-162^-1553248TrueTrue
p3109_k8p4uf8451271262^-187/4*2^-162^-1557344TrueTrue
p3109_k8p7sf87163630.01562563/32*2^-11127/64*2^0TrueTrue
p3109_k8p8uf8811271260.0078125127/64*2^-11127/64*2^0TrueTrue
binary161611515359163832^-241023/512*2^-152^-1465504TrueTrue
bfloat16168816255163832^-133127/64*2^-1272^-126255/128*2^127FalseTrue
ocp_e8m0818127127n/an/a2^-1272^127FalseTrue
ocp_int888063630.015625127/64*2^0n/an/aTrueTrue

Notes

All NaNs are the same, with no distinction between signalling or quiet, or between differently encoded NaNs.

About

Generic floating-point types in Python

Resources

Stars

20 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages