Skip to main content

Python fixed-point library..

Project description

PyPI Version Python Build Documentation Coverage Status python-versions semantic-versioning

PRs welcome Issues

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfxp-0.2.0.tar.gz (44.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyfxp-0.2.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file pyfxp-0.2.0.tar.gz.

File metadata

  • Download URL: pyfxp-0.2.0.tar.gz
  • Upload date:
  • Size: 44.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.26.0 CPython/3.13.7 Linux/6.11.0-1018-azure

File hashes

Hashes for pyfxp-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a51272e2d1c82072724d10f3d2408361b6622a699fa1fbdba942e881576ce286
MD5 c8258bb5a01f9de14822d59fa7851e64
BLAKE2b-256 7328f20153df3f623c6281172e442eb8069770a4dc90354c5c89ff4f8fdbb5f3

See more details on using hashes here.

File details

Details for the file pyfxp-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyfxp-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.26.0 CPython/3.13.7 Linux/6.11.0-1018-azure

File hashes

Hashes for pyfxp-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4821684d4022cf5b4ade39cff1711c67c9ff385bd8d012c8f469feacc449e28
MD5 aef1a0699ae97baf67f07e0700a7c966
BLAKE2b-256 f22269c0e594c17916485ef8b865c0f9d96c26dd509d27d3df1663a8864d07bb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page