Numerical model
The stages each predicate passes through, the exactness window within which Shewchuk’s proofs hold in binary64, and the tests that check the implementation. Together they establish that every sign arithmetica returns is exact.
The guarantee
Each predicate returns, for every finite input, the sign of the exact quantity it is defined by: the value its formula would have in real arithmetic, with no rounding at any step. For the classical predicates that quantity is a determinant, and for the distance and angle predicates a polynomial of the same kind. No range of magnitudes, no spread between the smallest and largest coordinate, and no degree of near-degeneracy exists beyond which the answer becomes approximate.
Certification establishes the guarantee. A predicate evaluates its quantity in a sequence of stages, and each stage either proves that its result has the sign of the exact value or passes the input to the next stage. No stage returns a sign it has not proved.
Why floating point fails
Each classical predicate is the sign of a determinant. For orient2d:
Near a degenerate configuration the two products are almost equal, and their difference is smaller than the rounding error of computing them. The rounded result can then be zero when the true value is not, or have the opposite sign. Code that branches on that sign, such as a triangulation, a polygon overlay or a point-in-polygon test, then builds a structure that is topologically impossible, with no indication that anything went wrong.
use arithmetica::{Sign, orient2d};
// The textbook formula, evaluated in plain f64.
let naive = |a: [f64; 2], b: [f64; 2], c: [f64; 2]| {
(a[0] - c[0]) * (b[1] - c[1]) - (a[1] - c[1]) * (b[0] - c[0])
};
// The third point is 2^-48 above the line y = x through the other two.
let (a, b, c) = ([0.5, 0.5], [12.0, 12.0], [24.0, 24.0 + 2f64.powi(-48)]);
assert_eq!(naive(a, b, c), 0.0); // rounding erased the answer
assert_eq!(orient2d(a, b, c), Ok(Sign::Positive)); // the exact sign
Shewchuk’s adaptive predicates resolve this with exact floating-point arithmetic, but his proofs assume an unbounded exponent range. Binary64 has a bounded one: a product can overflow to infinity, or underflow to a subnormal with fewer than 53 significant bits or to zero. At the extremes of the range the stages can then certify a sign that is wrong. Arithmetica closes that gap, and the next two sections describe how.
The stages
Every predicate passes through the same numbered stages, and the numbering is the same in every predicate’s documentation. Some predicates also have a zero filter ahead of Stage 1, which certifies an exactly degenerate input by comparisons alone and is not numbered.
| Stage | Method | What it does |
|---|---|---|
| 1 | Filter | Native f64 with the underflow-protected error bound of Bartels, Fisikopoulos &
Weiser (2023), Theorem 1(2). Valid for every finite input. |
| 2–3 | Adaptive expansions | Shewchuk’s exact products and sums, accumulated in floating-point expansions and, in the classical predicates, tested against his error bounds. They run only inside the exactness window. A predicate with a single expansion stage has a Stage 2 and no Stage 3. |
| 4 | Exact integers | The quantity evaluated exactly in fixed-width integer arithmetic. It answers input wider than the
window, and it confirms every Zero from the expansion stages. |
The zero filter
A zero filter certifies Zero from exact comparisons, with no arithmetic. orient2d
applies the zero filter of Bartels, Fisikopoulos & Weiser (2023), Definition 6, which certifies a
repeated point or an axis-aligned triple. dot_sign applies the same idea to its two products,
and compare_distance and compare_point_distance certify a repeated point. Under
gradual underflow a floating-point difference is zero only when its operands are equal, so each of these
comparisons is a proof rather than a heuristic.
Stage 1: the filter
The determinant d̃ is computed in plain f64, alongside a
magnitude M built from the same terms in the same order, with the smallest
normal number uN = 2−1022 added after every product.
The sign is accepted only if
where A is the predicate’s error-bound constant, derived for its own expression tree. The two absolute terms cover the error of products that land among the subnormals, where relative error bounds do not hold. The comparison is strict and the bound is never zero, so the filter never certifies an exactly degenerate input. Most ordinary input is decided here, for the cost of a few dozen floating-point operations.
Stages 2 and 3: adaptive expansions
An expansion is a sum of f64 values whose significands do not overlap, and it
represents a sum or product of f64 values exactly. In the four classical predicates, Stage 2
builds the determinant from exact products of the rounded coordinate differences and tests it against a
tighter bound, and Stage 3 adds the rounding errors of the differences themselves, giving the exact
determinant. These are Shewchuk’s algorithms. insphere has no Stage 3, so input that its
Stage 2 cannot decide goes to Stage 4.
The distance and angle predicates have a single expansion stage. It forms every difference exactly as a two-term expansion and every product exactly, then sums them, so the result is the exact value and its largest component carries the sign. That stage consults no error bound. Every expansion stage runs only on input inside the exactness window, below.
Stage 4: exact integers
Every finite f64 is an integer multiple of 2−1074, the
smallest subnormal. Scaling every coordinate by 21074 therefore turns the
determinant into an integer polynomial whose sign is the sign of the original:
Stage 4 evaluates that polynomial in fixed-width two’s-complement integers, sized in advance so that no intermediate value can overflow. The width follows from the degree:
| Predicate | Degree | Integer width (bits) |
|---|---|---|
orient2d |
2 | 4,224 |
orient3d |
3 | 6,336 |
incircle |
4 | 8,448 |
insphere |
5 | 10,560 |
compare_distance, dot_sign, compare_point_distance |
2 | 4,224 |
compare_segment_distance, interior case |
4 | 8,448 |
Integer arithmetic has no rounding error, so this stage needs no error bound. It costs about two orders of magnitude more than the filter, and input that is neither exactly degenerate nor spread across more bits than the window never reaches it. Stage 4 operates on the caller’s own coordinates, before any rescaling, so it decides exactly the caller’s question.
The exactness window
The exactness window is the range of magnitudes within which binary64 provably behaves as Shewchuk’s unbounded-exponent model does. For one call, let E be the largest exponent of a leading bit among the non-zero coordinates, and q the smallest exponent of a lowest set bit, so every coordinate is an integer multiple of 2q and smaller than 2E+1. For a predicate of degree d, the window is the set of inputs with
| Predicate | Degree d | Smallest q | Largest E | Widest spread E − q (bits) |
|---|---|---|---|---|
orient2d |
2 | −450 | 478 | 928 |
orient3d |
3 | −300 | 318 | 618 |
incircle |
4 | −225 | 238 | 463 |
insphere |
5 | −180 | 190 | 370 |
Inside the window, every operation of Stages 2 and 3 returns the same value in binary64 as it would with an unbounded exponent range, so Shewchuk’s theorems hold unchanged. The lower limit keeps every exact value on the 2−1074 grid and every error-bound product in the normal range, with a margin of 19 bits; the upper limit keeps every value below the overflow threshold, with a margin of 26 bits.
Outside the window, if the input’s spread E − q fits within the window’s width, every coordinate is multiplied by a single power of two. That multiplication is exact, and it multiplies the determinant by a positive number, so the sign is unchanged. If the spread is wider than the window, no scale factor works, and the call goes straight to Stage 4.
The distance and angle predicates reuse these windows by degree.
compare_distance, dot_sign and compare_point_distance are degree 2
and use orient2d’s window, and the interior case of compare_segment_distance
is degree 4 and uses incircle’s. The distance argument d counts as a
coordinate, so it enters E and q and is rescaled with the points. Their expansion stages
consult no error bound, so they keep a wider margin on the small side than the classical predicates
do.
For ordinary data the window is far away: any orient2d input whose non-zero coordinates lie
between about 2−398 and 2478 is inside it as given. The full statements and
proofs are in
docs/exactness_window.md in the repository.
Non-finite input
A NaN or infinite coordinate has no geometric meaning, so no sign is correct for it. Every predicate and
construction checks its input first and returns Err(NonFinite), rather than a
Sign::Zero that would be indistinguishable from a genuinely degenerate configuration.
A non-finite intermediate is a different matter. Finite coordinates near f64::MAX can
overflow a difference or a product to infinity, and that is not an error: the filter declines to certify
such a call, and the exact stages answer it like any other finite input.
Verification
The test suite is built around the inputs where a predicate can go wrong. A predicate can fail only where the true value is smaller than its rounding error, next to the surface on which the sign changes, and random inputs almost never land there.
- Boundary search: For each predicate and five magnitude profiles (ordinary, tiny, huge, mixed, and the window edges), one coordinate is bisected to the pair of adjacent floats on either side of a sign change, and the predicate is checked on those floats and their neighbors. A deep run makes about 18 million such calls with no wrong sign and no false zero.
- Exact oracles: Every expected sign is computed in arbitrary-precision integer or rational arithmetic, never in floating point.
- Fixed inputs at the extremes of the range: subnormal products, determinants far below the smallest subnormal, huge and tiny coordinates in one call.
- Fuzzing with
cargo-fuzzagainst the exact oracles. - Bound checks. Every rational inequality in the derivation of the distance predicates’ filter constants is verified exactly by the tests.
- Property tests with
proptest.
Tests find defects rather than prove their absence. The proof is the argument above and in docs/exactness_window.md. The tests check the one part
of it that rests on inspection rather than derivation, that the code implements Shewchuk’s algorithms
without an omitted term.
References
- Shewchuk, J. R. (1997). Adaptive precision floating-point arithmetic and fast robust geometric predicates. Discrete & Computational Geometry, 18(3), 305–363. https://doi.org/10.1007/PL00009321. The expansion arithmetic, the adaptive predicates and their error bounds.
- Shewchuk, J. R. (1996). Routines for arbitrary precision floating-point arithmetic
and fast robust geometric predicates (
predicates.c). Public domain. https://www.cs.cmu.edu/~quake/robust.html. The reference implementation from which the adaptive stages are ported. - Bartels, T., Fisikopoulos, V., & Weiser, M. (2023). Fast floating-point filters for robust predicates. BIT Numerical Mathematics, 63, 31. https://doi.org/10.1007/s10543-023-00975-x. The underflow-protected filter (Theorem 1(2)) and the zero filter (Definition 6).