python-fma
A Python fused multiply-add (FMA) binding with 32 and 64-bit float support and configurable IEEE 754 rounding modes.
Python 3.13+ provides math.fma, but it operates on float (IEEE 754 double)
only and does not expose the rounding direction. This module fills both gaps:
- Accepts
ct.c_double(float64) andct.c_float(float32). - Lets the caller select the rounding mode per-call.
- Detects and reports IEEE 754 floating-point exceptions (invalid, overflow, underflow, inexact, divide-by-zero).
- Restores the original FP rounding state after each operation.
Note
I write this project with vibe coding, but I carefully read every line in /src/ and /csrc/ folder, and I glanced at test code (and provides some data for rounding test), so the core logic should be ok.
The coverage test rate is high, and all non-platform-dependent code is covered in test.
Installation
$ pip install python-fma
The package ships a pre-built wheel for Linux, Windows and MacOS, support x86 and ARM architecture.
Requirements
- Python >= 3.10
if you want to build from source:
- C11 compiler
- CMake >= 3.25
API
fma(x, y, z, round=None, unsuppressed_exception=...)
import ctypes as ct
from python_fma import fma, DivByZeroException, OverflowException
r = fma(ct.c_double(2.0), ct.c_double(3.0), ct.c_double(1.0))
# r.value == 7.0
| Parameter | Type | Default | Description |
|---|---|---|---|
x, y, z |
ct.c_double or ct.c_float |
– | Operands (must all be the same type) |
round |
str or None |
None |
Rounding mode (see below). None leaves the current FP mode unchanged. |
unsuppressed_exception |
set of exception classes |
{DivByZeroException, OverflowException} |
Exception types allowed to propagate; all others are silently suppressed. |
Rounding mode strings:
| Name | Behaviour |
|---|---|
"FE_TONEAREST" |
Round to nearest, ties to even |
"FE_UPWARD" |
Round toward +infinity |
"FE_DOWNWARD" |
Round toward -infinity |
"FE_TOWARDZERO" |
Truncate toward zero |
Returns: ct.c_double or ct.c_float (same type as the inputs).
Raises:
TypeError— inputs are not all the same type or not a supported type.ValueError— unrecognised rounding-mode string.FeatureNotCompiledError— the rounding mode or an exception flag was not detected at compile time.UnsupportedRoundingModeError— the platform does not support the requested rounding mode at runtime.DivByZeroException,OverflowException— by default; other exceptions (InvalidException,UnderflowException,InexactException) are suppressed unless explicitly included inunsuppressed_exception.
exception_compiled(name)
Return whether detection of the floating-point exception name is available in
the C build.
from python_fma import exception_compiled, InvalidException
exception_compiled(InvalidException) # True / False
round_mode_compiled(name)
Return whether the rounding mode name was detected at compile time.
from python_fma import round_mode_compiled
round_mode_compiled("FE_UPWARD") # True / False
round_mode_supported(name)
Return whether the rounding mode name is supported at runtime.
from python_fma import round_mode_supported
round_mode_supported("FE_TOWARDZERO") # True / False
Exception hierarchy
Exception
└── FloatException
├── InvalidException (FE_INVALID)
├── DivByZeroException (FE_DIVBYZERO)
├── OverflowException (FE_OVERFLOW)
├── UnderflowException (FE_UNDERFLOW)
├── InexactException (FE_INEXACT)
├── FeatureNotCompiledError (query/compile-time support)
└── UnsupportedRoundingModeError (also a ValueError)
Usage examples
Basic FMA
import ctypes as ct
from python_fma import fma
r = fma(ct.c_double(2.0), ct.c_double(3.0), ct.c_double(1.0))
# 2.0 * 3.0 + 1.0 = 7.0
Single-precision FMA
r = fma(ct.c_float(1.5), ct.c_float(2.0), ct.c_float(0.5))
# r.value == 3.5, type(r) is ct.c_float
Explicit rounding mode
r = fma(ct.c_double(1.0), ct.c_double(1.0), ct.c_double(0.1),
round="FE_UPWARD")
Controlling which exceptions propagate
# Only allow InvalidException to propagate; suppress everything else.
r = fma(ct.c_double(float("inf")), ct.c_double(0.0), ct.c_double(0.0),
unsuppressed_exception={InvalidException})
Querying compile-time feature support
from python_fma import exception_compiled, round_mode_compiled, InvalidException
if not round_mode_compiled("FE_UPWARD"):
print("FE_UPWARD not available in this build")
if not exception_compiled(InvalidException):
print("Invalid exception detection not available in this build")
Development
Setup
$ python -m venv .venv
$ source .venv/bin/activate
$ pip install -e .
Lint and type-check
$ ruff check src/ tests/
$ ruff format --check src/ tests/
$ npx pyright src/ tests/
Test
$ python -m pytest tests/ -v
Build a wheel
$ pip install build
$ python -m build
CI / Docker build
Manylinux wheels are built via Docker:
$ docker build \
--build-arg BASE_IMAGE=quay.io/pypa/manylinux2014_x86_64 \
-t builder .
$ docker run --rm -v "$PWD/wheelhouse:/wheelhouse" builder
Windows and macOS wheels are built natively on GitHub Actions runners. See
.github/workflows/build.yml for the full CI matrix.
Project structure
python-fma/
├── csrc/ # C source (fma_module.c, fma_module.h, CMakeLists.txt)
├── src/
│ └── python_fma/
│ ├── __init__.py # Public Python API
│ └── _binding.py # ctypes bindings to the C library
├── badges/ # generated coverage badge
├── tests/ # pytest test suite
├── pyproject.toml # Build configuration (scikit-build-core, ruff)
├── README.md # This file
├── Dockerfile # manylinux wheel builder
└── .github/workflows/ # CI workflows
License
MPL
Metadata
Release files for python-fma 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 158.4 kB
Release files / python_fma-1.0.0-py3-none-win_arm64.whl
| Download URL | python_fma-1.0.0-py3-none-win_arm64.whl |
|---|---|
| Size | 17.2 kB |
| Tags | Python 3 Windows ARM64 |
|
SHA-256 checksum How to use checksums |
77f321206906bd02a499f29b87632621e253c247e7e30ea3beb272cfd22e20ae
|
|
BLAKE2b-256 checksum How to use checksums |
5b5c726a1662474aaa1ba9567f7f881e135fc0fa9af68092e8ba88b664caf767
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logRelease files / python_fma-1.0.0-py3-none-win_amd64.whl
| Download URL | python_fma-1.0.0-py3-none-win_amd64.whl |
|---|---|
| Size | 18.0 kB |
| Tags | Python 3 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
dea6b1e3f8fac17a6b5d170a6d87ab7b05b9f6c382ccda53417bc44ac33007d2
|
|
BLAKE2b-256 checksum How to use checksums |
276a107b9202fc925f27dff9d51c6c7da5523a690b6fbd9bb8f34c9d4d4d7fbf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logRelease files / python_fma-1.0.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
| Download URL | python_fma-1.0.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
|---|---|
| Size | 15.7 kB |
| Tags | Linux glibc 2.17+ ARM64 Python 3 |
|
SHA-256 checksum How to use checksums |
2de5377976b6e2002618854ce63131dbd7d8211abe30a8e77f832a3b509a8d90
|
|
BLAKE2b-256 checksum How to use checksums |
79c60678ba4bc296e952d6dc0f2cee12e53ff9db551c47172f3af3ad91bfd620
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logRelease files / python_fma-1.0.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_34_aarch64.whl
| Download URL | python_fma-1.0.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_34_aarch64.whl |
|---|---|
| Size | 15.9 kB |
| Tags | Linux glibc 2.17+ ARM64 Linux glibc 2.34+ ARM64 Python 3 |
|
SHA-256 checksum How to use checksums |
2c814f4bcdd2856d0bf7053d1395d0606e2f629ef90f15de3d632ac852c7b500
|
|
BLAKE2b-256 checksum How to use checksums |
91bfe3b3a34172783848b08ebace99a5c495b4644f46e1cb2b83050f2499295d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logRelease files / python_fma-1.0.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
| Download URL | python_fma-1.0.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 15.9 kB |
| Tags | Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 Python 3 |
|
SHA-256 checksum How to use checksums |
6a77ffe3b7d03449ed31845709e92ff963de3dfda75e9fd58ece44fffc2d7acb
|
|
BLAKE2b-256 checksum How to use checksums |
a7aadf15f71799016a1d8af4451f45e832e5ba56619cc8bab3f079ca636dbc1d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logRelease files / python_fma-1.0.0-py3-none-manylinux1_x86_64.manylinux_2_34_x86_64.manylinux_2_5_x86_64.whl
| Download URL | python_fma-1.0.0-py3-none-manylinux1_x86_64.manylinux_2_34_x86_64.manylinux_2_5_x86_64.whl |
|---|---|
| Size | 15.7 kB |
| Tags | Linux glibc 2.34+ x86-64 Linux glibc 2.5+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
157ccef32d5844778dc8c590eacb11d51ac0babfa83f0c5de8e31b8d50839d7e
|
|
BLAKE2b-256 checksum How to use checksums |
74d4db8902c718430c4b9f53487c371def5ae67dd0b4094c87c7f182528f9fa1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logRelease files / python_fma-1.0.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
| Download URL | python_fma-1.0.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl |
|---|---|
| Size | 15.7 kB |
| Tags | Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
b9ceb7cc2005f93b65a1b69892131bc5755257ce5f0c44080724039eb55e5122
|
|
BLAKE2b-256 checksum How to use checksums |
8abd26176100c6852ff5b53723c5e16baedd9538631eec8342c830ec78b4bbd2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logRelease files / python_fma-1.0.0-py3-none-manylinux1_x86_64.manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_5_x86_64.whl
| Download URL | python_fma-1.0.0-py3-none-manylinux1_x86_64.manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_5_x86_64.whl |
|---|---|
| Size | 15.7 kB |
| Tags | Linux glibc 2.17+ x86-64 Linux glibc 2.5+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
154e0cf92136d318f002feaef8c45e3858f67f47a1b94ec733adbe0421417f8e
|
|
BLAKE2b-256 checksum How to use checksums |
781a6ef5d9fd9cea078698837d6cf00e8a08789a532f042410301e47fdf233d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logRelease files / python_fma-1.0.0-py3-none-macosx_26_0_x86_64.whl
| Download URL | python_fma-1.0.0-py3-none-macosx_26_0_x86_64.whl |
|---|---|
| Size | 14.2 kB |
| Tags | Python 3 macOS 26.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
fdfdd3f0b2d5633c9451ea7439e005e2ada2e7e392d88501709d5ac803481b51
|
|
BLAKE2b-256 checksum How to use checksums |
deee604844cc1a2456673cb7e39864fe0434efaed37038633c541aa17fb5c52b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logRelease files / python_fma-1.0.0-py3-none-macosx_15_0_arm64.whl
| Download URL | python_fma-1.0.0-py3-none-macosx_15_0_arm64.whl |
|---|---|
| Size | 14.4 kB |
| Tags | Python 3 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
75df694e10caf5689e8dc35e73994189f194facd747b7edceaaa783bf053a638
|
|
BLAKE2b-256 checksum How to use checksums |
884fb1ed9498bf5736fd0f16102910042fd81c3bb8ef8a0a87236a0315fb1420
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logSecurity for python-fma 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 10 files in this release have been uploaded with provenance metadata.
Attestation for python_fma-1.0.0-py3-none-win_arm64.whl
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 1, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.