/anyfloat

Arbitrary precision floating point numbers and their bit-level representations in Python

Primary LanguagePythonApache License 2.0Apache-2.0

anyfloat

Python package for working with arbitrary* precision floating point numbers and their bit-level representations following IEEE-754 rules.

*: currently limited by machine precision, usually 64 bit

Usage

pip install anyfloat
>>> from anyfloat import FP8_E4M3
>>> FP8_E4M3.float_from_bitstring('01001000')
3.0
>>> from anyfloat import FloatingPointSpec
>>> FP4 = FloatingPointSpec(num_mantissa_bits=1, num_exponent_bits=2)
>>> FP4.float_from_bitstring('0110')
3.0
>>> FP4.bitstring_from_float(3.0)
0110

For more examples, see examples/.

Development

Quick start

Using a Python virtual environment is recommended

Install requirements:

pip install -r requirements.txt
pip install -r requirements-dev.txt

Install this package in editable mode:

pip install -e .

Install pre-commit hooks:

pre-commit install

Contributing

This repository follows Google's Python Style Guide.