gMath

Formats, precision & build

The formats, their storage and compute words, the rounding rules per domain, how accuracy is validated, and how to build.

Formats

A format is a type, Fixed<S, F>: a storage word S and F fraction bits. Every format is compiled into every build and any number of them can be used in one program (0.6.x chose one per build through GMATH_PROFILE; see UPGRADING_0.7.md).

Type Storage Compute Fraction bits ~Decimal digits Range
Fixed<i32, F>, e.g. Q16_16 i32 i64 any F in 2..=30 0.3 F ±2^(31 - F)
Q32_32 = Fixed<i64, 32> i64 i128 32 9 ±2^31
Q64_64 = Fixed<i128, 64> i128 I256 64 19 ±2^63
Q128_128 = Fixed<I256, 128> I256 I512 128 38 ±2^127
Q256_256 = Fixed<I512, 256> I512 I1024 256 77 ±2^255

The 32-bit word moves its binary point anywhere from Q30.2 to Q2.30: Fixed<i32, 10> (Q22.10, range about ±2 million, 3 digits) and Fixed<i32, 24> (Q8.24, range ±128, 7 digits) are as available as Q16.16. Generic code takes the format as a parameter (T: FixedNum, or Fixed<S, F> with S: Storage).

use g_math::fixed_point::{Fixed, Q16_16, Q256_256};

let a = Fixed::<i32, 10>::from_str("1234.5");   // Q22.10
let b = Q16_16::from_str("1234.5");
let c = Q256_256::from_str("1234.5");
assert_eq!(a.raw(), 1_264_128);                   // 1234.5 * 2^10, exact
assert_eq!(b.raw(), 80_904_192);                  // 1234.5 * 2^16, exact
assert!(c.sqrt().to_string().starts_with("35.1354521815"));

A clean release build compiles every format in about 35 seconds.

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 Fixed, vectors, matrices, tensors and manifold points (as bytes)
rebuild-tables regenerate lookup tables from build.rs

There is no feature per format and none around core functionality: every format, domain, transcendental, wide integer (I256 to I2048) and the tiered overflow are always compiled. The ternary inference kernels are the separate crate g_math_inference.

The full determinism and cross-format 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.