Algorithm Search⌘K

Proofs: Ryu

These are the human proofs of Ryu, at full length.

Ryu returns the shortest, closest decimal in the rounding interval full

This proof shows: correctness.

Setting. x=v⋅2e2x = v \cdot 2^{e_2} is a positive binary64 number with v=4m2v = 4m_2, and its rounding interval IxI_x has ends L=u⋅2e2L = u \cdot 2^{e_2} and H=w⋅2e2H = w \cdot 2^{e_2} with u=v−1−σu = v - 1 - \sigma, w=v+2w = v + 2, σ∈{0,1}\sigma \in \{0, 1\}. Both ends belong to IxI_x when m2m_2 is even and neither does when m2m_2 is odd. So H−x=2⋅2e2H - x = 2 \cdot 2^{e_2} and x−L=(1+σ)2e2≤H−xx - L = (1 + \sigma) 2^{e_2} \le H - x.

What is assumed. (A1) Line L3 computes the exact floors vm=⌊L/10E⌋vm = \lfloor L/10^E \rfloor, vr=⌊x/10E⌋vr = \lfloor x/10^E \rfloor, vp=⌊H/10E⌋vp = \lfloor H/10^E \rfloor at the starting exponent E=e10E = e_{10}. This is Adams's table-precision result. (A2) The flags of lines L4, L5 and L7 tell exactly whether L/10EL/10^E and x/10Ex/10^E are integers, in every case where that changes the output. For e2<0e_2 < 0 this is exact: x/10e10=v⋅5−e2−q/2qx/10^{e_{10}} = v \cdot 5^{-e_2 - q}/2^q is an integer iff 2q∣v2^q \mid v, and uu, ww are not multiples of 4, so for q≥2q \ge 2 the ends are never integers at that scale. For e2≥0e_2 \ge 0 the cutoff q≤21q \le 21 is taken from the reference implementation. The tests check (A1) and the final output in exact rationals.

For a scale 10k10^k, call an integer nn admissible at kk if n⋅10k∈Ixn \cdot 10^k \in I_x.

Lemma 1 (shortest decimals). Let k∗k^\ast be the largest kk such that some integer is admissible at kk. Then the decimals in IxI_x with the fewest significant digits are exactly the n⋅10k∗n \cdot 10^{k^\ast} with nn admissible at k∗k^\ast.

Proof. IxI_x is narrower than xx itself, so H<2LH < 2L, and the leading decimal positions of numbers in IxI_x differ by at most one. Write λ(y)\lambda(y) for the position of the leading digit of yy and τ(y)\tau(y) for the position of its last nonzero digit, so yy has λ(y)−τ(y)+1\lambda(y) - \tau(y) + 1 significant digits. Every y∈Ixy \in I_x has τ(y)≤k∗\tau(y) \le k^\ast by maximality. If IxI_x contains a power of ten 10t10^t, then t≤k∗t \le k^\ast and also 10t≤H<10t+110^t \le H < 10^{t+1}, so k∗=tk^\ast = t, the multiples of 10t10^t in IxI_x are among 10t,…,9⋅10t10^t, \ldots, 9 \cdot 10^t and all have one digit; one digit is the minimum. Otherwise every y∈Ixy \in I_x has the same λ(y)=λ\lambda(y) = \lambda, and the length λ−τ(y)+1\lambda - \tau(y) + 1 is minimal exactly when τ(y)=k∗\tau(y) = k^\ast, that is, when yy is a multiple of 10k∗10^{k^\ast} (a multiple of 10k∗10^{k^\ast} in IxI_x is not a multiple of 10k∗+110^{k^\ast + 1}). □\square

Lemma 2 (the start is fine enough). At the start, E=e10≤k∗E = e_{10} \le k^\ast, and either x/10Ex/10^{E} is an integer or E<k∗E < k^\ast.

Proof. For e2≥0e_2 \ge 0, q=⌊e2log⁡102⌋−[e2>3]q = \lfloor e_2 \log_{10} 2 \rfloor - [e_2 > 3] and 10e10=10q10^{e_{10}} = 10^q. For e2<0e_2 < 0, q=⌊−e2log⁡105⌋−[−e2>1]q = \lfloor -e_2 \log_{10} 5 \rfloor - [-e_2 > 1], e10=e2+qe_{10} = e_2 + q, and 10e10/2e2=10q/5−e210^{e_{10}}/2^{e_2} = 10^q/5^{-e_2}. In both cases 10e10≤2e210^{e_{10}} \le 2^{e_2}, and when the subtracted indicator is 1, 10e10≤2e2/1010^{e_{10}} \le 2^{e_2}/10. The interval [L,H][L, H] has length (3+σ)2e2≥3⋅2e2(3 + \sigma) 2^{e_2} \ge 3 \cdot 2^{e_2}, so its interior contains a multiple of 10e1010^{e_{10}}, which gives k∗≥e10k^\ast \ge e_{10}; when the indicator is 1 the interior has length at least 30⋅10e1030 \cdot 10^{e_{10}} and contains a multiple of 10e10+110^{e_{10}+1}, so k∗>e10k^\ast > e_{10}. When the indicator is 0, either 0≤e2≤30 \le e_2 \le 3, so q=0q = 0, E=0E = 0 and x=v⋅2e2x = v \cdot 2^{e_2} is an integer, or e2=−1e_2 = -1, so q=0q = 0, E=−1E = -1 and x/10−1=5vx/10^{-1} = 5v is an integer. □\square

Lemma 3 (the loops stop at k∗k^\ast). Keep the invariant that at exponent EE, vm=⌊L/10E⌋vm = \lfloor L/10^E \rfloor, vr=⌊x/10E⌋vr = \lfloor x/10^E \rfloor, and vpvp is the largest integer n≤H/10En \le H/10^E with n⋅10E≤Hn \cdot 10^E \le H allowed (that is, vp=⌊H/10E⌋vp = \lfloor H/10^E \rfloor, minus 1 if H/10EH/10^E is an integer and HH is excluded). Line L5 establishes it, and dividing by 10 preserves it because ⌊⌊y⌋/10⌋=⌊y/10⌋\lfloor \lfloor y \rfloor / 10 \rfloor = \lfloor y/10 \rfloor, while for an excluded integer end ⌊(n−1)/10⌋\lfloor (n - 1)/10 \rfloor is again the largest allowed integer below n/10n/10.

The integers in (vm,vp](vm, vp] are exactly the integers admissible at EE that are strictly above L/10EL/10^E. So ⌊vp/10⌋>⌊vm/10⌋\lfloor vp/10 \rfloor > \lfloor vm/10 \rfloor holds iff some multiple of 10 lies in (vm,vp](vm, vp], that is, iff some integer strictly above L/10E+1L/10^{E+1} is admissible at E+1E + 1. The only admissible integer it misses is L/10E+1L/10^{E+1} itself, when LL is included and is a multiple of 10E+110^{E+1}. Line L6 runs while the first kind exists; when it stops, no admissible integer at any coarser scale lies strictly above LL (each would give one at E+1E+1). Line L7 then continues exactly while the included end LL is a multiple of 10E+110^{E+1}; the flag tracks that L/10EL/10^E is an integer, and vm mod 10=0vm \bmod 10 = 0 tests the next digit. When both loops stop, no integer is admissible at E+1E + 1 and some is admissible at EE (by Lemma 2 at the start, and by the loop condition after each step). Hence E=k∗E = k^\ast. □\square

Lemma 4 (rounding). At E=k∗E = k^\ast let r=x/10E∈[vr,vr+1)r = x/10^{E} \in [vr, vr + 1). Line L6 records ℓ\ell, the first digit of r−vrr - vr, and whether all later digits are zero. Then r−vr>1/2r - vr > 1/2 iff ℓ≥6\ell \ge 6 or (ℓ=5\ell = 5 and some later digit is nonzero), and r−vr=1/2r - vr = 1/2 iff ℓ=5\ell = 5 and all later digits are zero. Lines L8 and L9 therefore choose vr+1vr + 1 exactly when r−vr>1/2r - vr > 1/2, or r−vr=1/2r - vr = 1/2 and vrvr is odd, or vrvr is not admissible; this needs at least one digit to have been dropped, or rr to be an integer, which Lemma 2 guarantees.

The candidate chosen is admissible. If vrvr is not admissible, then vr⋅10E<Lvr \cdot 10^E < L (it is at most xx), and since some integer is admissible and all admissible integers exceed vrvr, the least of them is vr+1vr + 1. If vr+1vr + 1 is chosen by rounding but were not admissible, then H≤(vr+1)10EH \le (vr + 1)10^E, so H−x<10E/2H - x < 10^E/2, hence x−L≤H−x<10E/2x - L \le H - x < 10^E/2 and L>vr⋅10EL > vr \cdot 10^E; then neither vrvr nor vr+1vr + 1 is admissible, and no other integer is (they lie outside [L,H][L, H]), contradicting E=k∗E = k^\ast. □\square

Theorem. Under (A1) and (A2), RYU returns d⋅10Ed \cdot 10^E with E=k∗E = k^\ast and dd the admissible integer at k∗k^\ast nearest to x/10k∗x/10^{k^\ast}, ties to even. By Lemma 1 this is the shortest decimal in IxI_x and the closest to xx among the shortest. dd is not divisible by 10, since otherwise d/10d/10 would be admissible at k∗+1k^\ast + 1.

Proof. Lemmas 2 and 3 give E=k∗E = k^\ast; Lemma 4 gives the choice of dd. The nearest admissible integer to rr is vrvr or vr+1vr + 1, because these are the integers on either side of rr and whichever is nearer and admissible is chosen. □\square

Back to Ryu