Table of Contents

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 any IBinaryInteger<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 any INumber<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 (BigInteger unscaled value plus an int scale). Use it for exact decimal values that exceed System.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 parallel Combine merge, 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 any IFloatingPointIeee754<T> component type (float, double, Half); the generic counterpart of the double-only System.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 companion Bodu.Financial package. Bodu.Financial depends on Bodu.Numerics so Money<T> can hand off to Fraction<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

  1. Working with Fraction<T> - the rational type and its arithmetic surface.
  2. Formatting and parsing Fraction<T> - once values are flowing, control how they render and what text round-trips.
  3. Working with Interval<T> - the interval type, independent of fractions; read in any order.
  4. Running and moving statistics - the stream accumulators and rolling windows; independent of the other types.
  5. Working with Complex<T> - the generic complex number; independent of the other types.
  6. JSON serialization - persist the value types; read last, after the value semantics are familiar.

See also