Repository navigation
Chainfix 0.2.0 adds exact binary fixed-point arithmetic for real and complex scalars, and optional NumPy arrays with ordered reductions. It contains breaking changes; read Upgrading from 0.1.x before upgrading.
Highlights
- Binary arithmetic (#13).
Fixb/Ufixbsupport+,-,*, reflected integer operations, unary sign andabs, exact comparisons and hashing, raw shifts, and power-of-two scaling. ImmutableFormatandMathdescriptors control result growth, all six rounding modes, and raise/saturate/wrap overflow. Use an explicitmath=per call, orlocal_binary_contextfor a scope. - Complex scalars (#14). New
CFixb: exact complex binary fixed point with one shared signed format, mixed complex/real/integer operators, conjugation,mag2, quantization, and exact equality and hashing. - Arrays (#18). Optional
chainfix.arrays, installed withpip install "chainfix[arrays]"(NumPy >= 2.5). Real and interleaved complex arrays support broadcasting, arithmetic, comparisons, and disjointout=buffers, using checked int64 kernels. The core package still does not depend on NumPy. - Ordered reductions (#24).
cfa.sum,cfa.dot,cfa.matmul,Array.sum, and@, plusnp.sum,np.add.reduce,np.dot, andnp.matmuldispatch. Each reduction has separate product, accumulator, and sum quantization stages. - Faster complex gain (#21). Lower-allocation conversion kernels speed up public array paths, for example complex
out=multiplication and nearest-even quantization. Seedocs/buffer-reuse.md. - Exact decimal construction (#27).
Fixd/Ufixdround once from the exact input value under the decimal context'smath, acceptFractionand fixed-point inputs, and compare and hash exactly against binary, complex, and Python numbers.str()gives the exact decimal value, andrepr()rebuilds the value exactly. - Packaging (#28). Apache-2.0 metadata in the PEP 639 format (
License-Expression), and updated project URLs.
Upgrading from 0.1.x
- Python 3.12 or newer is required. Support for Python 3.8–3.11 is dropped. Tested on 3.12 through 3.15.
- Binary overflow defaults to RAISE. Construction and conversion follow the context's rounding and overflow modes, or an explicit
math=. To saturate or wrap on purpose, configureMathexplicitly. - Exact conversion. Floats convert from their exact binary value and round once. Some results differ from 0.1.x, which converted through floats. For example,
Fixd(2.675, 32, 2).intis267, matchingdecimal.Decimal. - Exact equality and hashing. Fixed-point values compare and hash by exact numeric value, across formats and bases.
- Contexts are immutable (#26).
BinaryContextandDecimalContextare final. Assigning to them raisesAttributeError, and subclassing raisesTypeError. Usectx.replace(...)together withset_*_contextorlocal_*_context; both scopes also work as decorators.ctx.copy()andget_default()are removed.set_*_contextinstalls the given context rather than a copy.- The default contexts are frozen, so there is no process-wide default to change. Use
contextvars.copy_context().run,Thread(context=...)on Python 3.14+, or an explicitmath=. - Contexts pickled by earlier versions don't load, and contexts can't be weakly referenced.
DecimalContextdrops its unusedoverflowsetting and now carriesmath, which defaults to nearest-even rounding with raise on overflow.
- Removed internals.
_Fix,_Ufix,FromTypes, thedefault_*helpers, andget_current_context()are removed. - Error handling.
FixedPointOverflowErrorsubclassesValueError, so existingexcept ValueErrorhandlers still catch overflow. - Decimal arithmetic and direct
decimal.Decimalconstruction input are still unsupported. For finite values, useFraction(Decimal(...)).
Full documentation: README, binary arithmetic, arrays.
Full changelog: v0.1.2...v0.2.0