Keep Exact Values Exact Across Process Boundaries
Preserve rational values, domains, and comparison rules when mathematical results cross serialization boundaries, and keep display rounding out of verification.
Find the first representation that loses information
In an illustrative round trip, an exact solver emits the JSON number 9007199254740993. A consumer parses it as an IEEE 754 binary64 value using round-to-nearest, ties-to-even and serializes it back as 9007199254740992. The output is valid JSON, and a downstream exact integer parser faithfully preserves the wrong integer. Converting to arbitrary precision after the first parse cannot recover the missing bit.
RFC 8259 section 6 identifies integers from -(2^53 - 1) through 2^53 - 1, inclusive, as interoperable across implementations using binary64 precision. From 2^53 inclusive to 2^54 exclusive, successive binary64 values are two apart. The integer one above the base rounds down to the base, while the integer three above it rounds up to four above it. Both lie halfway between representable values. Ties-to-even chooses the one whose significand has an even low bit.
Figure 1 — Illustrative calculation under IEEE 754 binary64. Both axes show offsets from 2^53. Inputs 0 and 1 land at offset 0, input 2 stays at 2, and inputs 3 and 4 land at 4. A downstream cache keyed on the parsed number merges each of the two pairs.
Read as an exact decimal, the JSON token 0.1 denotes 1/10. Binary64 cannot hold that value because a finite binary fraction has a power-of-two denominator in lowest terms and ten contains a factor of five. A serializer can still print the binary approximation as 0.1, since that string rounds back to the same float. Exact decimal interpretation requires access to the original token or an explicitly exact wire representation.
Put the invariant on the wire
For rational results, use a tagged pair of decimal integer strings. For example, {"kind":"rational","numerator":"9007199254740993","denominator":"1"} carries the solver's exact integer as a rational. Validate the field types and integer grammar before parsing directly into arbitrary-precision integers. Use ASCII digits, with an optional minus sign on a nonzero numerator and no leading zeros. Require a positive denominator and a greatest common divisor of one. This forces zero to 0/1, since the greatest common divisor of zero and a positive denominator is the denominator itself.
A strict wire decoder can reject noncanonical fractions, while an import adapter can accept 4/6 and emit 2/3. That gives the protocol a single representation per rational and lets the import adapter handle user conventions. A string field supplies that protection only if intermediaries keep it a string. Exercise the actual path through gateways and client libraries, especially schema layers that coerce strings of digits to numeric fields.
In repeated arithmetic, even reduced numerators and denominators can grow. Adding terms with pairwise coprime denominators can leave their product in the reduced denominator. Larger integers take more bytes on the wire and more CPU time to parse, multiply, and reduce by greatest common divisor. Set digit limits from the workload and surface a capacity error if a result exceeds them.
Choose the smallest representation that preserves the promised value. A tagged decimal with signed integer digits and an explicit scale suffices when every result crossing the boundary has a terminating decimal and fits the contract's scale range. An exact binary64 bit pattern encoded as a fixed-width hex string suffices when the contract promises to reproduce the producer's stored float, including signed zero if it matters. Use full rationals when callers need exact results from divisions such as 1/3, or when a verifier must distinguish a gap from a cutoff that falls between adjacent values of a smaller representation.
Canonical values give a cache stable inputs. A key built from bytes also needs deterministic encoding and field ordering. A cached comparison depends on the relation and its parameters, so include the effective policy parameters or an immutable identifier that resolves to them. Otherwise two consumers can decode identical operands and still disagree about what a cached pass means.
Symbolic results also depend on the domain in which they were established. Put the domain and assumptions in the wire payload and the cache key, just as you would the parameters of a comparison policy. Over the reals, sqrt(x^2) simplifies to |x|. A service returning x must carry the precondition x >= 0 so a caller with an unrestricted real variable cannot reuse the conditional rewrite.
Treat the tolerance as an operand
A symmetric policy for two values in common units can use |a-b| <= max(atol, rtol * max(|a|, |b|)). Encode both nonnegative parameters as exact rationals. The parameter rtol is dimensionless, while rtol * max(|a|, |b|) and atol carry the operands' units. For a multiplicative change of units, explicitly rescale atol by the same positive factor as the operands and issue a new policy version for the converted units. In the illustrative policy below, converting metres to millimetres multiplies the operands by 1000, so atol = 1/1000 becomes atol = 1, while rtol stays 1/1000. An origin shift changes the magnitude used by this policy and calls for a separate decision about the reference scale.
Take an illustrative policy with atol = 1/1000 and rtol = 1/1000. Comparing 2 and 1.998 puts the exact gap and the relative allowance at 2/1000, so the pair passes. Replacing 1.998 with 1.9979 raises the gap to 21/10000, so it fails. A verifier that takes the smaller magnitude as its scale rejects even the first pair because its relative allowance falls below 2/1000.
Approximate agreement also fails transitivity. Under the same policy, the exact decimal values 0, 0.00075, and 0.0015 pass in adjacent pairs and fail at the extremes. If a cache uses a passing comparison to merge each new value with an existing representative, inserting the middle value first can put both extremes in the same group. Key on canonical exact values and use the tolerance when evaluating a specified pair.
Ship certified bounds with approximate results
Serialize certified enclosure endpoints as exact rationals and specify that the unknown value lies in the closed interval. When converting endpoints to binary64 for computation, round the lower one down and the upper one up. For example, round-to-nearest moves the lower endpoint of [1/10, 1/8] above 1/10. The converted enclosure then excludes a true value of exactly 1/10.
In the illustrative worked example below, outward rounding of 1/3 to adjacent thousandths gives [333/1000, 334/1000]. The exact gap from the reference 333/1000 is 1/3000, and the enclosure bounds that gap by [0, 1/1000]. A verifier that receives only the enclosure can certify agreement at the inclusive cutoff 1/1000 (0.001). At 1/2000, part of the enclosure falls outside the tolerance band, so the verifier needs more evidence even though the exact value passes.
Figure 2 — Illustrative worked example. Zero on the horizontal axis is the reference 333/1000. The exact gap is 1/3000, or two ticks. The enclosure reaches six ticks to the right of zero, ending on the boundary of the ±1/1000 band and three ticks outside the ±1/2000 band.
The nearest binary64 value to 0.001 exceeds 1/1000 by about 2.08 × 10^-20, calculated from its exact binary fraction. Both cutoffs accept the original enclosure, but widening the exact gap bound to 0.00100000000000000001 separates their decisions. This upper bound exceeds the rational cutoff and falls below the binary64 cutoff. A verifier that rounds the policy parameter to binary64 can therefore certify agreement where the intended policy requires more evidence.
For a fixed absolute tolerance, accept if the upper bound on the gap is at most the cutoff and reject if the lower bound exceeds it. Under the combined policy, check the upper gap bound against a lower bound on the allowance, and check the lower gap bound against an upper bound on the allowance. If neither test decides the comparison, refine the operand enclosures or bound the difference between the gap and the allowance as a function of the same operands.
Using the largest allowance to certify agreement can give a false pass. In a worked example with a in [1, 3], b = 2, atol = 0, and rtol = 1/3, the gap can reach 1. The largest allowance is also 1, attained at a = 3. At a = 1 the gap is still 1, but the allowance is only 2/3. The allowance at the top of the enclosure cannot justify accepting the value at the bottom.
Test the decoded fields and the verdict
Start the large-integer fixture with literal JSON bytes containing the tagged rational. A fixture assembled from a host number can lose the bit before the serializer under test ever runs. Check both the outbound field type and the exact decoded numerator at each hop to catch a gateway that coerces the string and then prints it back. Test normalization at the import adapter, where a supported spelling like "-0" can map to 0/1. Require the strict decoder to reject both negative zero and a zero denominator.
Pair the enclosure that lands exactly at the cutoff with the wider enclosure that separates rational and binary64 policy decoding. The first fixture catches a switch from inclusive to strict comparison. The second catches an accidental conversion of the tolerance to binary64. Test endpoint conversion separately with a true value on the lower bound. Comparing the decoded operands, endpoints, and parameters before checking the verdict locates which boundary introduced a discrepancy.
Compare before you format
An audit verifier that replays verdicts from formatted log lines can silently apply a different policy. In an illustrative replay test, an exact gap of 0.00104 fails the inclusive cutoff 0.001. A log formatter that rounds the gap to four decimal places prints 0.0010. Replaying from that field turns the failure into a pass. Record the exact operands or certified enclosures and the effective policy alongside the displayed gap so the verifier can reconstruct the decision.
Require each comparison record to carry the operand representations, their units and applicable domain assumptions, and the versioned relation with its exact parameters. For an inclusive absolute cutoff, accept when the certified upper bound on the gap is at or below the cutoff and reject when the lower bound is above it. Otherwise, return indeterminate and request tighter bounds.