Bodu.Numerics
Bodu.Numerics is a small numeric-primitives library that ships value
types covering common but missing gaps in the .NET BCL:
Fraction<T>- an immutable exact-rational number type generic over anyIBinaryInteger<T>backing component. Use it for accounting, precise decimal arithmetic, or anywhere floating-point rounding is unacceptable.Interval<T>- an immutable bounded interval generic over anyINumber<T>endpoint type, with independent open or closed endpoints on each side and full set algebra. Use it for guarded numeric ranges, validation predicates, bucketing, and reservation-style overlap checks.BigDecimal- an immutable arbitrary-precision decimal (BigIntegerunscaled value plus anintscale). Use it for exact decimal values that exceedSystem.Decimal's 28-29 digit precision or its exponent range, or that must preserve trailing-zero scale.RunningStatistics<T>/RunningQuantile<T>- single-pass, constant-space stream accumulators: Welford count/min/max/mean/variance with a parallelCombinemerge, and a P² streaming quantile estimator.MovingSum<T>/MovingMinMax<T>- rolling-window companions reporting the sum/mean and min/max of the most recent N samples in amortized O(1).Complex<T>- an immutable complex number over anyIFloatingPointIeee754<T>component type (float,double,Half); the generic counterpart of thedouble-onlySystem.Numerics.Complex, with the same arithmetic, elementary functions, and<real; imaginary>text form.
The value types (Fraction<T>, Interval<T>, BigDecimal, Complex<T>) are
readonly struct, value-equatable, and allocation-free in their
common paths; the statistics accumulators are mutable structs (with
class-based rolling windows). Everything integrates with the
generic-math interfaces that ship in .NET 8+.
Looking for
Money<TCurrency>, currencies, or FX? Those now live in the companionBodu.Financialpackage.Bodu.Financialdepends onBodu.NumericssoMoney<T>can hand off toFraction<BigInteger>for exact mid-chain arithmetic.
Guides
Working with Fraction<T>
Construction, canonical form, arithmetic, continued fractions, and rational approximation over any IBinaryInteger<T> backing type.
Formatting and parsing Fraction<T>
General, mixed-number, Unicode vulgar-fraction, and percentage specifiers; what the parser accepts; culture and span surfaces.
Working with Interval<T>
Endpoint inclusivity, the empty interval, membership and overlap, intersection and union, ISO 31-11 parsing and formatting.
Working with BigDecimal
Arbitrary-precision decimal arithmetic - the unscaled-value/scale model, exact add/subtract/multiply, division precision, rounding, and generic-math composition.
Working with Complex<T>
Construction and polar form, arithmetic, magnitude / phase / conjugate / reciprocal, the elementary functions pinned against System.Numerics.Complex, the <real; imaginary> text form, and the JSON wire shape.
JSON serialization
Round-tripping Fraction<T>, Interval<T>, DiscreteInterval<T>, IntervalSet<T>, BigDecimal, and Complex<T> through System.Text.Json - the Strict, Lenient, and Compact wire shapes and how to register them.
Interval algebra
The set-algebra surface of Interval<T> - intersection, union, difference, symmetric difference, unbounded bounds, the & / | operators, and the empty-interval rules that make the operations total.
Discrete integer intervals
DiscreteInterval<T> - the integer-domain interval with successor-aware emptiness and adjacency, distinct from the continuous Interval<T>.
Disconnected sets
IntervalSet<T> - a normalized union of disjoint intervals with N-ary union, intersection, difference, and complement over the whole line.
Running and moving statistics
RunningStatistics<T>, RunningQuantile<T>, and the rolling-window MovingSum<T> / MovingMinMax<T> - single-pass stream summaries, the mutable-struct usage rules, and the P² estimator's behaviour.
Generic math constraints
Writing code generic over Fraction<T> and Interval<T> through the .NET INumber<T> / IBinaryInteger<T> abstractions.
Reading path
- Working with
Fraction<T>- the rational type and its arithmetic surface. - Formatting and parsing
Fraction<T>- once values are flowing, control how they render and what text round-trips. - Working with
Interval<T>- the interval type, independent of fractions; read in any order. - Running and moving statistics - the stream accumulators and rolling windows; independent of the other types.
- Working with
Complex<T>- the generic complex number; independent of the other types. - JSON serialization - persist the value types; read last, after the value semantics are familiar.
See also
- Bodu.Numerics introduction - namespaces, headline types, scenarios.
- Bodu.Numerics getting started - install + minimal samples.
- Numerics & Financial topic guides - the topic map; the complete numerics guide list is this page, and the financial one is its index.
- Numerics & Financial topic overview - package boundaries and the decision table.
Fraction<T>API referenceInterval<T>API referenceBigDecimalAPI referenceComplex<T>API referenceRunningStatistics<T>API referenceRunningQuantile<T>API referenceIntervalstatic factory helpersBodu.Financialoverview - money, currency, FX.