1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// src/nearest/bounds.rs
//! Certified bounds on the squared distance from a point to a box.
//!
//! [`bounds_sq`] computes two `f64` values `L ≤ d² ≤ U`, where `d²` is the
//! **exact** squared Euclidean distance from the query point to the box, taken
//! as a real number. The bounds are certified, meaning the inequalities hold
//! for every finite query point and every valid [`Rect`]. The proof below is
//! given in full because the nearest-neighbor guarantee rests on nothing
//! else.
//!
//! # The exact quantity
//!
//! The point of a box `[x₀, x₁] × [y₀, y₁]` nearest to `p = (x, y)` is the
//! clamp of `p` to the box, a selection among input values decided by exact
//! comparisons. Per axis, the exact distance is
//!
//! ```text
//! dx = x₀ − x if x < x₀
//! x − x₁ if x > x₁
//! 0 otherwise (dy likewise; d² = dx² + dy²)
//! ```
//!
//! # The computation
//!
//! The computation takes `a = fl(dx)` (one subtraction, or exactly 0) and
//! `b = fl(dy)`, then `ŝ = fl(fl(a·a) + fl(b·b))`, with `fl` the IEEE 754
//! round-to-nearest of each operation. The bounds follow from `ŝ`:
//!
//! ```text
//! ŝ < T → L = 0, U = U_SMALL
//! ŝ infinite → L = fl(MAX · K_LO), U = +∞
//! otherwise → L = fl(ŝ · K_LO), U = fl(ŝ · K_HI)
//! ```
//!
//! Here `T = 2⁻¹⁰²⁰`, `U_SMALL = 2⁻¹⁰¹⁹`, `K_LO = 1 − 2⁻⁵⁰` and
//! `K_HI = 1 + 2⁻⁵⁰` (both factors exactly representable), and `MAX` is the
//! largest finite `f64`.
//!
//! # The proof
//!
//! Let `u = 2⁻⁵³`, the unit roundoff.
//!
//! 1. **Subtraction:** `a = dx(1 + δ₁)` with `|δ₁| ≤ u`, by correct rounding.
//! When the exact difference of two floats is subnormal it is exactly
//! representable, so the subtraction never loses accuracy to underflow,
//! and either `|δ₁| ≤ u` or `a` is exact. If `dx` is at or above the
//! overflow threshold, `a = +∞`, a case item 6 resolves.
//! 2. **Squaring:** While `a²` is in the normal range,
//! `fl(a·a) = a²(1 + δ₂)`, `|δ₂| ≤ u`. When `a²` is subnormal the error
//! is absolute rather than relative, `fl(a·a) = a² + η` with
//! `|η| ≤ 2⁻¹⁰⁷⁵` (half the subnormal spacing).
//! 3. **Addition:** Both operands are non-negative, so
//! `fl(P + Q) = (P + Q)(1 + δ₃)`, `|δ₃| ≤ u`. A subnormal sum of
//! subnormal operands is exact.
//! 4. **Together:** With `η` the combined absolute term,
//! `|η| ≤ 2 · 2⁻¹⁰⁷⁵ = 2⁻¹⁰⁷⁴`, the computed sum is bounded on both sides:
//!
//! ```text
//! ŝ ∈ [ d²(1−u)⁴ − 2⁻¹⁰⁷⁴(1+u), d²(1+u)⁴ + 2⁻¹⁰⁷⁴(1+u) ]
//! ```
//!
//! Let `c = 2⁻¹⁰⁷³ ≥ 2⁻¹⁰⁷⁴(1+u)`. Rearranged, the two inequalities the
//! bounds must respect are
//! `d² ≥ (ŝ − c)/(1+u)⁴` and `d² ≤ (ŝ + c)/(1−u)⁴`.
//! 5. **`L` when `T ≤ ŝ < ∞`:** Since `ŝ ≥ T = 2⁻¹⁰²⁰`, the absolute term
//! satisfies `c ≤ u·ŝ` (`2⁻¹⁰⁷³ = 2⁻⁵³ · 2⁻¹⁰²⁰`). So
//! `d² ≥ ŝ(1−u)/(1+u)⁴`. The computed lower bound satisfies
//! `fl(ŝ·K_LO) ≤ ŝ(1−2⁻⁵⁰)(1+u) = ŝ(1−8u)(1+u)`, and
//! `(1−8u)(1+u)⁵ ≤ 1−3u+O(u²) < 1−u`, so `fl(ŝ·K_LO) < d²`. The product
//! cannot underflow (`ŝ·K_LO ≥ T/2` is normal) and cannot overflow
//! (`K_LO < 1`).
//! 6. **Overflow:** `ŝ = +∞` requires the exact `a² + b²` (or `dx` itself)
//! to reach the rounding threshold `2¹⁰²⁴(1 − 2⁻⁵⁴)`, so
//! `d² ≥ 2¹⁰²⁴(1−2⁻⁵⁴)/(1+u)⁴ ≥ 2¹⁰²⁴(1 − 5u)`. The mapped bound is
//! `fl(MAX·K_LO) ≤ 2¹⁰²⁴(1−u)(1−8u)(1+u) < 2¹⁰²⁴(1−8u) < d²`. `U = +∞`
//! bounds anything from above. Infinities never meet a subtraction that
//! could make NaN, because every operand along the way is non-negative,
//! so `ŝ` is `+∞` or finite and never NaN.
//! 7. **`L` when `ŝ < T`:** `L = 0 ≤ d²` always. The bound depends on the
//! threshold. Without it, `ŝ = 2⁻¹⁰⁷⁴` (a subnormal square rounded up)
//! would give `fl(ŝ·K_LO) = 2⁻¹⁰⁷⁴` back (the product rounds up), which
//! exceeds the true `d²` when `d² < 2⁻¹⁰⁷⁴`.
//! 8. **`U` when `T ≤ ŝ < ∞`:** Symmetrically, `d² ≤ ŝ(1+u)/(1−u)⁴` and
//! `fl(ŝ·K_HI) ≥ ŝ(1+8u)(1−u)`, with `(1+8u)(1−u)⁵ ≥ 1+2u > 1+u`, so
//! `fl(ŝ·K_HI) > d²`. If the product overflows, `U = +∞`, still valid.
//! 9. **`U` when `ŝ < T`:** `d² ≤ (ŝ + c)/(1−u)⁴ < (T + c)(1 + 5u) < 2T`, so
//! the constant `U_SMALL = 2T = 2⁻¹⁰¹⁹` bounds `d²` from above. The
//! constant is required rather than a convenience, since `ŝ` can be 0
//! while `d² > 0` (a square that underflows), and `fl(ŝ·K_HI)` would
//! then be 0, below `d²`.
//!
//! **The computation ends without a directed-rounding step.** A `next_down` on
//! `L` (and `next_up` on `U`) would serve the same inequality that `K_LO`
//! (`K_HI`) already serves with margin. The proof in items 5 and 8 closes
//! with `1−8u` against a need of about `1−5u`, strictly, so a directed step
//! could never separate a correct bound from an incorrect one. A component
//! whose removal no test can detect is either unnecessary or untested, and
//! this one would be unnecessary.
//!
//! # Monotonicity
//!
//! The lower bound is monotone in the box: **if `B ⊇ B′` then
//! `L(p, B) ≤ L(p, B′)`**. Per axis, the exact distance to `B` is at most the
//! exact distance to `B′`, because an enclosing interval is never farther
//! from `p`. Every step of the computation is monotone non-decreasing.
//! Correctly rounded subtraction is `fl` of the exact difference, and `fl`
//! is monotone. Squaring is monotone on non-negative values, and so are the
//! addition, the multiplication by a positive constant, the threshold step
//! (below `T` the bound is 0, at or above `T` it is at least
//! `fl(T·K_LO) > 0`), and the overflow mapping (`fl(MAX·K_LO)` is the value
//! the finite branch gives at `ŝ = MAX`, the largest finite `ŝ`). `L` is
//! therefore a monotone function of the per-axis exact distances.
//!
//! The search rests on this property. A node's box contains every
//! descendant's box, so the node's `L` is at most every descendant entry's
//! `L`, and a best-first search over `L` can stop without descending
//! further. A non-monotone step would not make a bound wrong; it would
//! break that stopping rule, and the broken rule would produce a false
//! negative.
use crateRect;
/// The value below which the absolute underflow term `c = 2⁻¹⁰⁷³` need not
/// be at most `u·ŝ`. Below it the bounds come from the two constants
/// instead (proof, items 7 and 9).
const UNDERFLOW_THRESHOLD: f64 = f64MIN_POSITIVE * 4.0; // 2⁻¹⁰²⁰, exact
/// The upper bound used when `ŝ` is below the threshold, `2⁻¹⁰¹⁹`, which is
/// at or above every `d²` consistent with such an `ŝ` (proof, item 9).
const UPPER_BELOW_THRESHOLD: f64 = f64MIN_POSITIVE * 8.0; // exact
/// The downward factor `1 − 2⁻⁵⁰`, exactly representable (proof, item 5).
const K_LO: f64 = 1.0 - f64EPSILON * 4.0;
/// The upward factor `1 + 2⁻⁵⁰`, exactly representable (proof, item 8).
const K_HI: f64 = 1.0 + f64EPSILON * 4.0;
/// The lower bound when `ŝ` overflows, the finite branch's value at the
/// largest finite `ŝ` (proof, item 6, and monotonicity).
const LOWER_AT_OVERFLOW: f64 = f64MAX * K_LO;
/// The exact per-axis distance, rounded once, `fl(dx)`.
///
/// Exact comparisons of stored values decide the branch, and only the
/// subtraction rounds (proof, item 1). The result is `0.0` when `p` is
/// within the interval, ends included.
/// `ŝ = fl(fl(a·a) + fl(b·b))` for the two per-axis distances.
/// Certified bounds `(L, U)` with `L ≤ d² ≤ U`, `d²` the exact squared
/// distance from the finite point `(x, y)` to `rect` (module doc).
///
/// `L` is finite and non-negative, and `U` is positive or `+∞`. Neither is
/// ever NaN. `L` is monotone, so a box containing another never receives
/// the larger `L`.
pub
pub
pub