Welcome to Chainfix
Chainfix brings fixed-point data types to Python for two kinds of application: binary (base-2) fixed point for digital signal processing (DSP), and decimal (base-10) fixed point for financial applications.
Chainfix provides real and complex binary fixed-point arithmetic and exact decimal fixed-point construction, inspection, and comparison. The scalar package uses Python integers and has no NumPy dependency. The optional array interface adds NumPy storage, checked integer arithmetic, reusable output destinations, and ordered sum, dot, and matrix-product reductions.
import chainfix as cf
q14 = cf.Format(base=2, signed=True, wordlength=16, precision=14)
a = cf.Fixb(0.75, format=q14)
b = cf.Fixb(0.5, format=q14)
c = cf.Fixb(0.25, format=q14)
y = a * b + c # full product and sum growth; exactly 0.625
dsp = cf.Math(rounding="floor", overflow="wrap", product=q14, sum=q14)
with cf.local_binary_context(math=dsp):
bounded = a * b + c
Values carry their format; arithmetic follows the active scope or an explicit
math= argument. Defaults are nearest-even rounding and raise on overflow.
See
binary arithmetic
for conversion rules, growth, scopes, supported operators, and compatibility notes.
Complex scalars share one signed format for their real and imaginary components:
z = cf.CFixb(0.5 + 0.25j, format=q14)
mixed = z * z.conjugate() + a
power = z.mag2() # exact squares and sum, then one sum-policy boundary
assert z.real.int == 8192 and z.imag.int == 4096
See complex arithmetic for exact component construction, mixed real/complex arithmetic, and growth rules.
Fixed-point data types are used in a wide range of computing applications. Binary-fixed point types are commonly used in the design of specialized hardware, FPGAs, ASICS, and signal processing applications.
Decimal-fixed point types are commonly found in financial applications, blockchain, decentralized finance, and smart contracts (e.g. the solidity programming language).
Binary fixed-point representations
The real-world value π can be represented with limited precision using two bytes in binary fixed point format
>>> from chainfix import *
>>> from math import pi
>>> pib = Fixb(pi, 16, 12)
>>> pib
Fixb(3.1416015625, 16, 12)
The .int property returns the stored integer value:
>>> pib.int
12868
The resolution of the binary fixed-point data type is 2 ** -precision, which
is also the value of one least significant bit:
>>> pib.lsb
0.000244140625
The range of numbers that can be represented with this precision using 16 bits is:
>>> (pib.lower_bound, pib.upper_bound)
(-8.0, 7.999755859375)
The .hex property returns the two's complement representation of the stored integer
>>> pib.hex
'0x3244'
When Fixb is used to store a negative number, the MSB of the stored integer is always 1:
>>> Fixb(-pi, 16, 4).hex
'0xffce'
Decimal fixed-point representations
Likewise, π can also be represented with limited precision using two bytes in decimal fixed point format
>>> pid = Fixd(pi, wordlength=16, precision=4)
>>> pid
Fixd(Fraction('3.1416'), 16, 4)
>>> print(pid)
3.1416
str() shows the exact decimal value. repr() is a constructor call that rebuilds
the value exactly, so a value that is not exactly a float appears as a Fraction.
The .int property returns the stored integer value:
>>> pid.int
31416
The resolution of the decimal fixed-point data type is 10 ** -precision, which
is also the value of one least significant bit:
>>> pid.lsb
0.0001
The range of numbers that can be represented with this precision using 16 bits is:
>>> (pid.lower_bound, pid.upper_bound)
(-3.2768, 3.2767)
The real-world value can also be displayed as an exact ratio of integers
>>> pid.as_integer_ratio()
(3927, 1250)
The .hex property returns the two's complement representation of the stored integer
>>> pid.hex
'0x7ab8'
When Fixd stores a negative number, the MSB of the stored integer is always 1:
>>> Fixd(-pi, 16, 4).hex
'0x8548'
Construction is exact and rounds once. A float converts as its exact binary value,
the same way decimal.Decimal(x) does, so a float that looks like a decimal tie
may not be one. Use a Fraction for an exact decimal literal:
>>> Fixd(2.675, 16, 2).int # the float is 2.67499999999999982236431605997495353221893310546875
267
>>> from fractions import Fraction
>>> Fixd(Fraction("2.675"), 16, 2).int # an exact tie, rounded to even
268
Rounding and overflow come from the active decimal context (by default
nearest_even and raise) or an explicit math= argument. Decimal values compare
and hash exactly, with each other, with binary values, and with Python numbers.
Contexts
A context supplies the default wordlength and precision for new values and a
policy (math): its rounding and overflow apply during construction, and binary
contexts also bound arithmetic results. Contexts are immutable: derive a
changed one with replace() and activate it for a block or for the rest of the
current execution context. There is no process-wide default to reconfigure.
The active context can be retrieved using
>>> ctx = get_decimal_context()
>>> ctx.wordlength
256
>>> ctx.precision
18
for Fixd and Ufixd values or
>>> ctx = get_binary_context()
>>> ctx.wordlength
32
>>> ctx.precision
16
for Fixb, Ufixb, and CFixb values.
Scope a changed context to a block; the previous context is restored on exit, even when the block raises:
>>> with local_decimal_context(wordlength=20, precision=5):
... Fixd(pi)
Fixd(Fraction('3.14159'), 20, 5)
Or install one for the rest of the current execution context:
>>> set_decimal_context(get_decimal_context().replace(wordlength=20, precision=5))
>>> Fixd(pi)
Fixd(Fraction('3.14159'), 20, 5)
local_binary_context, set_binary_context, and BinaryContext.replace() work
the same way and also accept policy settings such as rounding="floor" or
math=Math(...). Decimal contexts accept rounding, overflow, and math,
which apply when decimal values are constructed, but not result boundaries.
Assigning to a context attribute raises AttributeError.
When passed through the constructor, the values for wordlength and precision always take precedence over the
context.
>>> with local_decimal_context(wordlength=20, precision=5):
... Fixd(pi, precision=3)
Fixd(Fraction('3.142'), 20, 3)
The explicit precision overrides the active context; its word length must still provide enough range for the value.
Each asyncio task and each copied contextvars context has its own binding, so
installing a context in one never changes another. An empty execution context
uses the library defaults. Whether a new thread starts with an empty or an
inherited context depends on the Python version and build; to run work in a
thread with the caller's context on any version, submit it as
executor.submit(contextvars.copy_context().run, fn).
Decimal arithmetic and division remain unsupported.
Contributing
See CONTRIBUTING.md for PR titles, squash merging, and release policy.
Package Installation
Python 3.12 or newer is required. The scalar package has no runtime dependencies. The optional array interface requires NumPy >=2.5:
pip install 'chainfix[arrays]'
# Create virtual environment (recommended)
python3 -m venv venv
source venv/bin/activate
# Install in development mode
pip install -e .
# Install testing tools
pip install pytest pytest-cov tox
Testing
Running bare tox requires both Python 3.12 and Python 3.15 to be installed and
discoverable by tox. Use tox -e py312-nonumpy to run with Python 3.12 alone.
Missing interpreters are treated as errors so an incomplete matrix is visible.
# Run all tests
pytest
# Run the supported CI environments against an installed wheel
tox
# Run just the scalar-only Python 3.12 environment
tox -e py312-nonumpy
| Tox environment | Python | NumPy test dependency |
|---|---|---|
py312-min |
3.12 | Exactly 2.5.0 |
py315-latest |
3.15 | Latest compatible stable release, >=2.5 |
py312-nonumpy |
3.12 | Not installed |
All three environments run the scalar suite against an installed wheel. The min and latest environments install the arrays extra and run the array tests too; nonumpy verifies scalar independence and the missing-array-dependency message. Array test modules skip collection when NumPy is absent. CI uploads each environment's coverage with a distinct flag, which Codecov combines for the commit.
License
Copyright 2021-2026 MikesRND. Licensed under the Apache License, Version 2.0.
Metadata
Release files for chainfix 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| chainfix-0.2.0.tar.gz | 91.5 kB | With provenance | Details for chainfix-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| chainfix-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for chainfix-0.2.0-py3-none-any.whl |
Total release size: 134.3 kB
Release files / chainfix-0.2.0.tar.gz
| Download URL | chainfix-0.2.0.tar.gz |
|---|---|
| Size | 91.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cc41548b3b988bac9187c407ecd4d567bb31f13016de81331aafe55473002743
|
|
BLAKE2b-256 checksum How to use checksums |
d82fee02ff99941dfe8f0bddf3c6433040da26f6b8d2de0d98e7adc76339d672
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 15, 2026.
Transparency logRelease files / chainfix-0.2.0-py3-none-any.whl
| Download URL | chainfix-0.2.0-py3-none-any.whl |
|---|---|
| Size | 42.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6f4428c8b18e7d5607b9dd7c333bf74f04bd870d926217245584679fcdad7149
|
|
BLAKE2b-256 checksum How to use checksums |
93629dd89900d252ae7d6fe40b9801a516b85dfb9ebabc2b849e2b2103d093ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 15, 2026.
Transparency logSecurity for chainfix 0.2.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
Both files in this release have been uploaded with provenance metadata.
Attestation for chainfix-0.2.0.tar.gz
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 Nov 15, 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.