Algorithm Search⌘K

Proofs: Dragon4 (Steele-White)

These are the human proofs of Dragon4 (Steele-White), at full length.

Dragon4 prints the shortest, nearest decimal in the rounding interval full

This proof shows: correctness.

Setting. Binary64, x>0x > 0 finite, x=m⋅2qx = m \cdot 2^{q} as returned by L1 (m<253m < 2^{53}, q≥−1074q \ge -1074, and m≥252m \ge 2^{52} unless q=−1074q = -1074). Let x−x^{-} and x+x^{+} be the neighbors of xx (with x+=21024x^{+} = 2^{1024} for the largest finite number), g−=(x−x−)/2g^{-} = (x - x^{-})/2 and g+=(x+−x)/2g^{+} = (x^{+} - x)/2, and let RR be the rounding interval [x−g−,x+g+][x - g^{-}, x + g^{+}] when mm is even and (x−g−,x+g+)(x - g^{-}, x + g^{+}) when mm is odd. RR is the set of reals that a reader with round to nearest, ties to even, maps to xx (IEEE 754-2019, Clause 4.3.1; for the largest finite number the upper end is the overflow threshold of Clause 7.4). "Inside RR" means in this interval, closed or open according to the parity of mm; this is the variable inclusive. A decimal y>0y > 0 has nn significant digits if y=N⋅10ty = N \cdot 10^{t} with integers N,tN, t and 10n−1≤N<10n10^{n - 1} \le N < 10^{n}.

Lemma 1 (setup, L2). After L2, r/s=xr/s = x, m+/s=g+m^{+}/s = g^{+} and m−/s=g−m^{-}/s = g^{-}.

Proof. If m≠252m \ne 2^{52} or q=−1074q = -1074, both neighbors are at distance 2q2^{q}: in a binade the floats are the multiples of 2q2^{q}, and at q=−1074q = -1074 the subnormals and the smallest binade share that spacing. Then g±=2q−1g^{\pm} = 2^{q - 1}, and the two branches give r/s=2m2q/2r/s = 2m2^{q}/2 or 2m/21−q2m/2^{1 - q}, equal to xx, and m±/s=2q/2m^{\pm}/s = 2^{q}/2 or 1/21−q1/2^{1 - q}, equal to 2q−12^{q-1}. If m=252m = 2^{52} and q>−1074q > -1074, then xx is a power of two, x+−x=2qx^{+} - x = 2^{q} and x−x−=2q−1x - x^{-} = 2^{q - 1}, so g+=2q−1g^{+} = 2^{q - 1} and g−=2q−2g^{-} = 2^{q - 2}; the branches give r/s=4m2q/4r/s = 4m2^{q}/4 or 4m/22−q4m/2^{2 - q}, equal to xx, m+/s=2q+1/4m^{+}/s = 2^{q + 1}/4 or 2/22−q2/2^{2 - q}, equal to 2q−12^{q - 1}, and m−/s=2q−2m^{-}/s = 2^{q - 2}. For the largest finite number, m=253−1m = 2^{53} - 1 and g+=2q−1g^{+} = 2^{q - 1} matches the virtual neighbor 210242^{1024}. □\square

In every later step rr, m+m^{+}, m−m^{-} and ss are multiplied by the same powers of ten in a way that L3 and L4 record in kk, and L5 multiplies the first three by 1010 once per digit. Define, after the jj-th execution of L6, the integer DjD_{j} whose decimal digits are the digits produced so far, the last one being dd before any adjustment in L9. The next lemma is the invariant.

Lemma 2 (invariant). After L3 and L4, r/s=x⋅10−kr/s = x \cdot 10^{-k} and m±/s=g±⋅10−km^{\pm}/s = g^{\pm} \cdot 10^{-k}. After the jj-th execution of L6, 0≤r<s0 \le r < s and

x⋅10j−k=Dj+rs,g±⋅10j−k=m±s.x \cdot 10^{j - k} = D_{j} + \frac{r}{s}, \qquad g^{\pm} \cdot 10^{j - k} = \frac{m^{\pm}}{s}.

Proof. L3 multiplies ss by 10k10^{k} when k≥0k \ge 0, and rr, m±m^{\pm} by 10−k10^{-k} when k<0k < 0; each iteration of L4 multiplies ss by 1010 and adds 11 to kk. This gives the first claim from Lemma 1. For the second, induct on jj: before step jj, x⋅10j−1−k=Dj−1+r/sx \cdot 10^{j - 1 - k} = D_{j - 1} + r/s with D0=0D_0 = 0 and 0≤r<s0 \le r < s (for j=1j = 1, r/s=x⋅10−k<1r/s = x\cdot 10^{-k} < 1 by Lemma 3 below). L5 makes r/sr/s ten times larger, and L6 writes 10r=ds+r′10r = ds + r' with 0≤r′<s0 \le r' < s, so d≤9d \le 9 and x⋅10j−k=10Dj−1+d+r′/s=Dj+r′/sx \cdot 10^{j - k} = 10 D_{j - 1} + d + r'/s = D_{j} + r'/s. The relation for m±m^{\pm} is multiplied by 1010 on both sides. A step that continues (L8) keeps dd as the last digit, so DjD_j is as defined. □\square

Lemma 3 (the exponent, L3 and L4). After L4, kk is the least integer such that every point of RR is below 10k10^{k}. In particular x<10kx < 10^{k}, and RR contains no number ≥10k\ge 10^{k}.

Proof. Let e2=q+ℓ(m)−1e_2 = q + \ell(m) - 1, where ℓ(m)\ell(m) is the length of mm in binary, so 2e2≤x<2e2+12^{e_2} \le x < 2^{e_2 + 1}. The constant c=30103/100000c = 30103/100000 satisfies 0<c−log⁡102<5⋅10−90 < c - \log_{10} 2 < 5 \cdot 10^{-9}, and ∣e2∣≤1075|e_2| \le 1075, so ∣e2c−e2log⁡102∣<10−5|e_2 c - e_2\log_{10} 2| < 10^{-5}. Hence the estimate k0=⌊e2c⌋−1≤e2c−1<e2log⁡102≤log⁡10xk_0 = \lfloor e_2 c\rfloor - 1 \le e_2 c - 1 < e_2 \log_{10} 2 \le \log_{10} x, so 10k0<x10^{k_0} < x. By Lemma 2 applied before the digit loop, the test of L4, r+m+>sr + m^{+} > s or (inclusive and r+m+=sr + m^{+} = s), says exactly that the upper end x+g+x + g^{+} of RR is above 10k10^{k}, or equal to it with the end included, that is, that RR has a point ≥10k\ge 10^{k}. This property is monotone in kk: it holds for k0k_0 (since x>10k0x > 10^{k_0} and x∈Rx \in R) and fails for large kk. L4 increments kk until it fails, so the final kk is the least integer for which it fails. The loop runs at most 33 times, since x+g+≤2e2+1x + g^{+} \le 2^{e_2 + 1} gives log⁡10(x+g+)≤(e2+1)log⁡102<k0+3.31\log_{10}(x + g^{+}) \le (e_2 + 1)\log_{10} 2 < k_0 + 3.31. □\square

Lemma 4 (what the tests mean). After step jj, let yj−=Dj⋅10k−jy_{j}^{-} = D_{j} \cdot 10^{k - j} and yj+=(Dj+1)⋅10k−jy_{j}^{+} = (D_{j} + 1)\cdot 10^{k - j}. Then yj−≤x<yj+y_{j}^{-} \le x < y_{j}^{+}, these are the consecutive multiples of 10k−j10^{k - j} around xx, and low holds if and only if yj−y_{j}^{-} is inside RR, high if and only if yj+y_{j}^{+} is inside RR.

Proof. The bracketing is Lemma 2 with 0≤r<s0 \le r < s. Multiplying by s⋅10k−js \cdot 10^{k-j}, the test r<m−r < m^{-} reads x−yj−<g−x - y_{j}^{-} < g^{-}, that is yj−>x−g−y_{j}^{-} > x - g^{-}, and equality r=m−r = m^{-} is the lower end itself, admitted only when inclusive. Since yj−≤xy_{j}^{-} \le x, this is exactly "yj−y_{j}^{-} is inside RR" (for Dj=0D_j = 0 the test fails, consistent with 0∉R0 \notin R because g−<xg^{-} < x). Likewise r+m+>sr + m^{+} > s reads yj+<x+g+y_{j}^{+} < x + g^{+}, with equality at the upper end. □\square

Lemma 5 (numbers with few digits). For n≥1n \ge 1, RR contains a number with at most nn significant digits if and only if yn−y_{n}^{-} or yn+y_{n}^{+} is inside RR.

Proof. By Lemma 3, every point of RR is below 10k10^{k}, and at k−1k - 1 the property of L4 holds, so RR has a point z0≥10k−1z_0 \ge 10^{k - 1}. Suppose y∈Ry \in R has n′≤nn' \le n significant digits. If y≥10k−1y \ge 10^{k - 1}, its leading digit is at position k−1k - 1 and its last nonzero digit at a position ≥k−n′≥k−n\ge k - n' \ge k - n, so yy is a positive multiple of 10k−n10^{k - n}. If y<10k−1y < 10^{k - 1}, then RR, an interval containing yy and z0z_0, contains 10k−110^{k - 1}, again a positive multiple of 10k−n10^{k - n}. So in both cases RR contains a positive multiple zz of 10k−n10^{k - n}. If z≤xz \le x then 0<z≤yn−≤x0 < z \le y_{n}^{-} \le x, and if z>xz > x then x<yn+≤zx < y_{n}^{+} \le z; since RR is an interval containing xx and zz, it contains yn−y_{n}^{-} or yn+y_{n}^{+} respectively. Conversely, yn±∈Ry_{n}^{\pm} \in R is a positive multiple of 10k−n10^{k - n} below 10k10^{k}, which has at most nn significant digits. □\square

Theorem. Let the loop stop after step jj and let NN be the integer whose digits are returned, so the output denotes y=N⋅10k−jy = N \cdot 10^{k - j}. Then:

  1. yy is inside RR, so a correctly rounding reader (ties to even) maps yy back to xx;
  2. no number with fewer than jj significant digits is inside RR, and yy has exactly jj significant digits, its first and last digits being nonzero;
  3. yy is the multiple of 10k−j10^{k - j} inside RR nearest to xx, with a tie broken to an even last digit; it is also the nearest to xx of all the numbers with jj significant digits inside RR.

Proof. By Lemma 4, the loop continues at step nn exactly when neither yn−y_{n}^{-} nor yn+y_{n}^{+} is inside RR, which by Lemma 5 means that RR contains no number with at most nn significant digits. So jj is the least nn for which RR contains a number with at most nn significant digits; this is claim 2 except for the shape of the output. L9 returns yj−y_{j}^{-} when only low holds, yj+y_{j}^{+} when only high holds, and one of them when both hold; in each case yy is inside RR, which is claim 1. yy is a positive multiple of 10k−j10^{k - j} below 10k10^{k}, so it has at most jj significant digits, and by the minimality of jj not fewer. Hence N≥10j−1N \ge 10^{j - 1} and 10∤N10 \nmid N (for j=1j = 1, 1≤N≤91 \le N \le 9). The returned string is Dj−1D_{j - 1} followed by dd or d+1d + 1; if d+1=10d + 1 = 10 then NN would be divisible by 1010, so the last digit is a single digit, and a leading zero in DjD_{j} would force N=Dj+1=10j−1N = D_j + 1 = 10^{j - 1}, divisible by 1010 for j≥2j \ge 2; so the string is exactly the decimal numeral of NN.

For claim 3, the multiples of 10k−j10^{k - j} nearest to xx on each side are yj−y_{j}^{-} and yj+y_{j}^{+}, and any other one inside RR is farther from xx. If both are inside, then x−yj−=(r/s)10k−jx - y_{j}^{-} = (r/s) 10^{k - j} by Lemma 2, so yj−y_{j}^{-} is nearer when 2r<s2r < s, yj+y_{j}^{+} when 2r>s2r > s, and they tie when 2r=s2r = s; L9 then chooses dd if it is even and d+1d + 1 otherwise, the even last digit. By the proof of Lemma 5, a number with jj significant digits inside RR that is not a multiple of 10k−j10^{k - j} lies below 10k−110^{k - 1}, and then 10k−1∈R10^{k - 1} \in R and j=1j = 1. Such a one-digit number z≤9⋅10k−2z \le 9 \cdot 10^{k - 2} could beat yy only if x<10k−1x < 10^{k - 1}; then RR contains zz and 10k−110^{k - 1}, so its width, which is at most 2q2^{q}, is at least 10k−210^{k - 2}, and m=x/2q<10k−1/10k−2=10m = x/2^{q} < 10^{k - 1}/10^{k - 2} = 10. So xx is one of the subnormals m⋅2−1074m \cdot 2^{-1074} with m≤9m \le 9. Among them only m=2m = 2 has x<10k−1x < 10^{k - 1}: there R=[3⋅2−1075,5⋅2−1075]≈[7.41,12.36]⋅10−324R = [3 \cdot 2^{-1075}, 5 \cdot 2^{-1075}] \approx [7.41, 12.36] \cdot 10^{-324}, k=−322k = -322, the output is 10−32310^{-323} at distance about 0.12⋅10−3240.12 \cdot 10^{-324} from x≈9.88⋅10−324x \approx 9.88 \cdot 10^{-324}, and the other one-digit candidates 8⋅10−3248 \cdot 10^{-324} and 9⋅10−3249 \cdot 10^{-324} are farther. ■\blacksquare

Remarks. The rounding interval is that of a reader with round to nearest, ties to even; for a reader that breaks ties differently, set inclusive from that rule. The proof uses only exact integer arithmetic after L1, so it does not depend on the rounding mode of the machine.

Back to Dragon4 (Steele-White)