gMath

Precision, profiles & build

Storage widths, the fractional-bit split, the rounding rules per domain, how accuracy is validated, and how to build.

Profiles

Storage width is a compile-time choice via the GMATH_PROFILE environment variable (default: embedded), or the equivalent Cargo feature.

Profile Format Storage Compute ~Decimal digits Intended use
realtime Q16.16 i32 i64 4 ML inference, real-time
compact Q32.32 i64 i128 9 mobile, game logic
embedded Q64.64 i128 I256 19 IoT, financial
balanced Q128.128 I256 I512 38 general services
scientific Q256.256 I512 I1024 77 research
cargo build                           # embedded (default)
GMATH_PROFILE=scientific cargo build  # 77-digit precision

Custom integer/fraction split (realtime only). The realtime profile can move the binary point inside the 32-bit storage word via GMATH_FRAC_BITS (valid range 2–30 fractional bits); the other profiles use fixed splits.

GMATH_FRAC_BITS=10 GMATH_PROFILE=realtime cargo build   # Q22.10: wider range, 3 digits
GMATH_FRAC_BITS=24 GMATH_PROFILE=realtime cargo build   # Q8.24:  narrow range, 7 digits

Switching profiles compiles different code paths via cfg flags. Clear the incremental cache first, or stale artifacts can crash:

rm -rf target/debug/incremental/

Lookup tables are checked in, so a default build is fast. --features rebuild-tables regenerates them from build.rs (pure-Rust generation: π via Machin's formula, e via factorial series, √2 via continued fractions, zero runtime dependencies) and is slow.

Rounding

Rounding is defined per domain, and it is deterministic (integer arithmetic, so identical on every platform). The key property: everything beyond a lone storage-tier multiply or divide (every transcendental, dot product, decomposition, matrix chain, and fused op) computes at tier N+1 with double the fractional bits and rounds to storage exactly once, so the wide→storage downscale is the only rounding the result sees. The normative per-domain rounding table and the full explanation are in the rounding contract, CONTRACT.md §3. The certified interval types are the deliberate exception: they round each endpoint outward (toward −∞ below, +∞ above) so that the bracket provably contains the exact result; the scalar paths keep the per-domain rules and always land inside the corresponding interval.

Validation

Accuracy is defined by the test suite, not slogans:

cargo test --release

Limits worth knowing

Feature flags

Flag Effect
infinite-precision BigInt tier for the symbolic rational domain (pulls in num-bigint)
serde Serialize/Deserialize for FixedPoint, vectors, matrices, tensors
inference TQ1.9 ternary inference ops + rayon parallel matvec
rebuild-tables regenerate lookup tables from build.rs
realtime / compact / embedded / balanced / scientific select profile via Cargo feature instead of GMATH_PROFILE
legacy-tests compile legacy test suites

No feature gates around core functionality: all domains, transcendentals, wide integers (I256/I512/I1024), and tiered overflow are always compiled.

The full determinism and cross-profile contract is in CONTRACT.md.

Disclaimer

This software is provided "as is", without warranty of any kind, express or implied. Use of this software is entirely at your own risk. In no event shall the author or contributors be held liable for any damages arising from the use or inability to use this software.


Built by Niels Erik Toren · support & donations.