Python fixed-point library..
Project description
pyfxp package
pyfxp brings fixed-point precision to Python — simple, fast, and explicit.
Define numbers in ARM-style Q-format, control every bit of precision, rounding, and overflow, and even JIT-compile your code with Numba for high-speed execution.
Key Features
- Clear, expressive API (fxp, fxpt, and Q)
- Configurable overflow & rounding modes (SAT, WRAP, HALF_EVEN, etc.)
- Numba-compatible — ready for high-performance, JIT-compiled fixed-point operations.
- Works with scalars and numpy arrays.
- No need to work with a custom fixed-point data-type.
- Perfect for DSP, control, and embedded algorithm prototyping.
Installation
Installing it is pretty easy:
pip install pyfxp
Quick Start
pyfxp offers two ways to convert numbers to a fixed-point format:
- fxpt → “typed” (parameter-based) Pass the format directly as function parameters.
- fxp → “spec-based” (explicit FxpSpec)
Build a reusable spec with
Q(...), then pass that spec.
Use the fxpt function when the format is simple or used in a single place:
from pyfxp import fxpt
from pyfxp.constants import SAT, HALF_EVEN # overflow & rounding modes
y = fxpt(
x=3.14159,
qi=3, # integer bits (including sign when signed=True)
qf=13, # fractional bits
signed=True, # signed or unsigned format
ovf=SAT, # overflow behavior: WRAP | SAT | ERROR
rnd=HALF_EVEN, # rounding: TRUNC, CEIL, TO_ZERO, AWAY, HALF_UP, ...
)
!!! note "ARM-style Q-format"
In ARM-style Q-format notation (used by pyfxp), the sign bit is included in the integer bit count qi.
Refer to Q (number format) for more information.
Use fxp when you want a reusable, readable definition. The fixed-point format is defined
using the Q(...) function once, and can be
reused for different fxp calls:
import numpy as np
from pyfxp import fxp, Q
from pyfxp.constants import SAT, HALF_EVEN
# Define the format once
Q3_13 = Q(qi=3, qf=13, signed=True, ovf=SAT, rnd=HALF_EVEN)
# Reuse it across your codebase
y1 = fxp(3.14159, Q3_13)
y2 = fxp(np.array([0.1, 0.2, 0.3]), Q3_13)
Q(...) returns an FxpSpec instance
which is basically a named tuple containing the specs used by fxp.
When specifying the fixed-point specs using either fxpt or Q,
all the following rounding and overflow modes are supported:
Rounding modes:
| Constant | Description |
|---|---|
TRUNC |
Bit truncation (rounds toward negative infinity) |
CEIL |
Round toward positive infinity |
TO_ZERO |
Round toward zero |
AWAY |
Round away from zero |
HALF_UP |
Round to nearest; ties toward positive infinity |
HALF_DOWN |
Round to nearest; ties toward negative infinity |
HALF_EVEN |
Round to nearest; ties to even |
HALF_ZERO |
Round to nearest; ties toward zero |
HALF_AWAY |
Round to nearest; ties away from zero |
Overflow modes:
| Constant | Description |
|---|---|
WRAP |
Wrap around on overflow (modular arithmetic) |
SAT |
Saturate at the maximum/minimum representable value |
ERROR |
Raise an exception on overflow |
The constants above are defined in the pyfxp.constants module.
They are used by both fxpt and fxp to specify fixed-point arithmetic behavior.
!!! tip "Usage example" These constants are simple numeric codes — meant to be readable and interoperable with compiled or vectorized backends (e.g. Numba or C extensions).
```python
from pyfxp.constants import SAT, HALF_EVEN
fxpt(1.25, qi=3, qf=13, ovf=SAT, rnd=HALF_EVEN)
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyfxp-0.2.1.tar.gz.
File metadata
- Download URL: pyfxp-0.2.1.tar.gz
- Upload date:
- Size: 44.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.26.1 CPython/3.13.7 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82cb266363e488650bd71cc1a90d3c994ce51895a7d236d60a860759bf8d83f2
|
|
| MD5 |
945d73c910a8c159571b100161d215c1
|
|
| BLAKE2b-256 |
49d7d861d5bd4844f98e116bac6a6cfc0c9dcf17fd5029057d3d3b3bb0743212
|
File details
Details for the file pyfxp-0.2.1-py3-none-any.whl.
File metadata
- Download URL: pyfxp-0.2.1-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.26.1 CPython/3.13.7 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9aae31902a60dc402b8b0abaae502f853731a8bc46812ba6bd33c603f9e4f63
|
|
| MD5 |
ef7b598c09f05897c590cd605b9a6ead
|
|
| BLAKE2b-256 |
9b0baa7c2566da1427e13c872c5b8dff84af56499f76ea118c3bdb2bb023b369
|