Expand description
The numeric tower’s foundation: a hand-rolled arbitrary-precision integer.
Logos carries the type of a number across every boundary (interpreter, VM,
wire), so an integer never silently becomes an IEEE-754 double the way a JSON
number does — there is no 2^53 cliff here. BigInt is the exact, unbounded
integer all of that rests on; Rational/Decimal/Complex build on it.
Representation is sign + little-endian base-2^64 magnitude (limbs, no trailing zeros; zero is the empty magnitude). A single-limb magnitude is stored inline — no heap allocation for anything that fits 64 bits, and the add/mul/div_rem fast paths compute those cases in machine registers. Arithmetic here is correct first — schoolbook add/sub/mul and bit-at-a-time long division — which is the exact-determinism floor; Karatsuba multiplication and Knuth-D division are the FAST follow-up that must reproduce these results bit-for-bit.
Structs§
- BigInt
- An exact, arbitrary-precision integer.
- Complex
- An exact complex number
re + im·i, each part aRational. Because the parts are exact,i·i == −1and(1+i)(1−i) == 2hold with no floating error, and the Gaussian rationals are closed under+ − × ÷(every nonzero value has an exact inverse). The magnitude√(re²+im²)is irrational in general — request it as anf64view viaComplex::abs_f64rather than forcing the exact value inexact. - Decimal
- An exact base-10 fixed-point number: an integer
coefficientdivided by a power of ten. The value iscoefficient · 10^(−scale), so19.99iscoefficient = 1999,scale = 2. Money lives here —0.1 + 0.2is exactly0.3, never the0.30000000000000004of a binary float, and a long ledger of sums never drifts. - Modular
- An element of the ring ℤ/nℤ — an integer taken modulo a fixed
modulus. This is the arbitrary-modulus generalisation ofcrate::Word8/…/crate::Word64(whose modulus is a power of two): arithmetic WRAPS into[0, modulus), the cyclic group where overflow is the point, not a bug. The number-theory / crypto substrate — modular exponentiation and the extended-Euclid inverse live here, so RSA-style and finite-field code is exact. - Rational
- An exact rational number: a fraction kept in lowest terms with a strictly
positive denominator. Built on
BigInt, so it never rounds the way a JSON /f64“number” does —1/3stays exactly1/3, not0.3333…, and a numerator past 2^53 survives instead of collapsing onto a double.
Enums§
- Rounding
Mode - How a
Decimalresolves the digits it must drop when rounding to a smaller scale.HalfEven(banker’s rounding) is the money/finance default: the unbiased tie-break that does not drift a long column of sums upward.
Constants§
- NUMERIC_
HASH_ P - The Mersenne prime 2^61 − 1: the modulus of the UNIFIED numeric hash.
Every numeric type hashes to its mathematical value mod P, so values that
compare equal across types (
1 == 1.0 == 1/1) hash equal — the hash/ equality coherence law that lets mixed-type map keys unify.
Functions§
- cmp_
bigint_ f64_ exact - Exact comparison of a BigInt against an f64.
Noneifffis NaN. - cmp_
i64_ f64_ exact - Exact comparison of an i64 against an f64.
Noneifffis NaN. - cmp_
rational_ f64_ exact - Exact comparison of a Rational against an f64.
Noneifffis NaN. - numeric_
hash_ bigint - The unified numeric hash of a BigInt. Limbs fold most-significant first: 2^64 ≡ 8 (mod 2^61 − 1).
- numeric_
hash_ f64 - The unified numeric hash of an f64:
m · 2^e mod Pvia modular exponentiation (2^61 ≡ 1, so the exponent reduces mod 61). NaN and the infinities take fixed sentinels outside the finite-value pattern. - numeric_
hash_ i64 - The unified numeric hash of an i64 (its value mod P, sign-adjusted).
- numeric_
hash_ rational - The unified numeric hash of a Rational:
num · den⁻¹ mod P(Fermat inverse; P is prime, and a reduced denominator is never ≡ 0 mod P for representable values). - rational_
from_ f64_ exact - The EXACT rational value of a finite f64.
Nonefor NaN/±inf.