gMath

Correct rounding for chains and geometry

Status: implemented on branch correct-rounding-chains (2026-10-15; section 7 records what was built and measured). Owner request: "We want to fix the chains too ... using tier+1 where possible so there is no intermediate rounding", and geometry "cut to 0". Builds on the full-range accuracy work (branch exp-full-range): every single binary transcendental is correctly rounded through a fast engine plus a sure path at about 1000 bits.

1. What is being promised

What is not promised: a result that is exactly a tie at storage precision and whose exactness cannot be shown within 1,900 bits is rounded as a tie (toward +infinity). For algebraic and transcendental functions of rational inputs this only happens when the value is exactly the tie.

2. Method: tracked fast pass, sure pass on demand (Ziv)

One evaluator, written once, generic over a numeric backend:

Every value carries an error bound 2^b in units of the backend's last place, propagated by first-order rules (integer bit arithmetic, no floats):

node bound of the result
leaf 0 if exact, else 1/2 unit
a + b, a - b ea + eb + 1/2
a * b abs(a) eb + abs(b) ea + ea eb + 1/2
a / b (ea + abs(a/b) eb) / (abs(b) - eb) + 1/2 (undecided if abs(b) <= eb)
exp x e^x (ex + engine)
ln x ex / (abs(x) - ex) + engine
sqrt x ex / (2 sqrt(x - ex)) + engine
sin, cos, atan, tanh, asinh ex + engine (derivative at most 1)
tan x (1 + tan^2) ex + engine
sinh, cosh e^abs(x) ex + engine
asin, acos ex / sqrt(1 - (abs(x) + ex)^2) + engine
acosh ex / sqrt((x - ex)^2 - 1) + engine
atanh ex / (1 - (abs(x) + ex)^2) + engine
pow(x, y) as exp(y ln x)

engine is each engine's measured maximum error at its backend (Compute: from a corpus against the sure path, recorded per function and format with a factor 4 of headroom; Big: the series truncation and rounding bounds of the sure algorithms, a few units of 2^-p relative).

At the root: if the discarded bits lie farther than the bound from a rounding boundary, the rounding is decided. Otherwise the tree is evaluated again on Big at 1,000 bits, then 1,900 bits. Trigonometric arguments beyond 2^24 and any bound that cannot be formed (division by a value not separated from zero) send the evaluation straight to Big.

3. Big: the sure backend as a floating type

BigFloat { neg, mant: I2048 (normalised, top bit at p - 1), exp: i64 }. Exact conversions from raws and decimal rationals (one rounded division); add/sub with a sticky bit, mul through a 4096-bit product split into two 2048-bit halves (schoolbook on words), div by long division at p bits, sqrt by integer square root, and exp, ln, sin/cos (reduction against pi held to p + exponent bits, computed once per precision by Machin), atan (half-angle steps plus series), the rest composed. Each function returns its value with its error bound in units of 2^-p relative.

4. Where it plugs in

5. Phases and gates

Phase Work Gate
CR1 BigFloat and its functions, bounds unit pins, every function vs mpmath at 1,000 bits on a corpus
CR2 tracked evaluator over Compute and Big, canonical binary chains random trees (depth 2 to 4 over the 17 functions, + - * /, decimal and binary leaves, pi and e) vs mpmath at seven formats, plus stress seeds
CR3 decimal chains the same trees with decimal output at D = 0..38
CR4 engine bounds measured and recorded; settle on them the full-range gate and its stress seeds
CR5 geometry primitives over the backend per-primitive mpmath corpora at seven formats; differential gate without tolerance (new == correctly rounded)
CR6 performance fast-path cost within 5 percent of today where decided; sure-pass frequency and cost reported per function and format

Each phase: the ten-build sweep before its commit, findings and docs in the same cycle.

6. Risks recorded up front

7. As built (2026-10-15)