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, finite, as returned by L1 (, , and unless ). Let and be the neighbors of (with for the largest finite number), and , and let be the rounding interval when is even and when is odd. is the set of reals that a reader with round to nearest, ties to even, maps to (IEEE 754-2019, Clause 4.3.1; for the largest finite number the upper end is the overflow threshold of Clause 7.4). "Inside " means in this interval, closed or open according to the parity of ; this is the variable inclusive. A decimal has significant digits if with integers and .
Lemma 1 (setup, L2). After L2, , and .
Proof. If or , both neighbors are at distance : in a binade the floats are the multiples of , and at the subnormals and the smallest binade share that spacing. Then , and the two branches give or , equal to , and or , equal to . If and , then is a power of two, and , so and ; the branches give or , equal to , or , equal to , and . For the largest finite number, and matches the virtual neighbor .
In every later step , , and are multiplied by the same powers of ten in a way that L3 and L4 record in , and L5 multiplies the first three by once per digit. Define, after the -th execution of L6, the integer whose decimal digits are the digits produced so far, the last one being before any adjustment in L9. The next lemma is the invariant.
Lemma 2 (invariant). After L3 and L4, and . After the -th execution of L6, and
Proof. L3 multiplies by when , and , by when ; each iteration of L4 multiplies by and adds to . This gives the first claim from Lemma 1. For the second, induct on : before step , with and (for , by Lemma 3 below). L5 makes ten times larger, and L6 writes with , so and . The relation for is multiplied by on both sides. A step that continues (L8) keeps as the last digit, so is as defined.
Lemma 3 (the exponent, L3 and L4). After L4, is the least integer such that every point of is below . In particular , and contains no number .
Proof. Let , where is the length of in binary, so . The constant satisfies , and , so . Hence the estimate , so . By Lemma 2 applied before the digit loop, the test of L4, or (inclusive and ), says exactly that the upper end of is above , or equal to it with the end included, that is, that has a point . This property is monotone in : it holds for (since and ) and fails for large . L4 increments until it fails, so the final is the least integer for which it fails. The loop runs at most times, since gives .
Lemma 4 (what the tests mean). After step , let and . Then , these are the consecutive multiples of around , and low holds if and only if is inside , high if and only if is inside .
Proof. The bracketing is Lemma 2 with . Multiplying by , the test reads , that is , and equality is the lower end itself, admitted only when inclusive. Since , this is exactly " is inside " (for the test fails, consistent with because ). Likewise reads , with equality at the upper end.
Lemma 5 (numbers with few digits). For , contains a number with at most significant digits if and only if or is inside .
Proof. By Lemma 3, every point of is below , and at the property of L4 holds, so has a point . Suppose has significant digits. If , its leading digit is at position and its last nonzero digit at a position , so is a positive multiple of . If , then , an interval containing and , contains , again a positive multiple of . So in both cases contains a positive multiple of . If then , and if then ; since is an interval containing and , it contains or respectively. Conversely, is a positive multiple of below , which has at most significant digits.
Theorem. Let the loop stop after step and let be the integer whose digits are returned, so the output denotes . Then:
- is inside , so a correctly rounding reader (ties to even) maps back to ;
- no number with fewer than significant digits is inside , and has exactly significant digits, its first and last digits being nonzero;
- is the multiple of inside nearest to , with a tie broken to an even last digit; it is also the nearest to of all the numbers with significant digits inside .
Proof. By Lemma 4, the loop continues at step exactly when neither nor is inside , which by Lemma 5 means that contains no number with at most significant digits. So is the least for which contains a number with at most significant digits; this is claim 2 except for the shape of the output. L9 returns when only low holds, when only high holds, and one of them when both hold; in each case is inside , which is claim 1. is a positive multiple of below , so it has at most significant digits, and by the minimality of not fewer. Hence and (for , ). The returned string is followed by or ; if then would be divisible by , so the last digit is a single digit, and a leading zero in would force , divisible by for ; so the string is exactly the decimal numeral of .
For claim 3, the multiples of nearest to on each side are and , and any other one inside is farther from . If both are inside, then by Lemma 2, so is nearer when , when , and they tie when ; L9 then chooses if it is even and otherwise, the even last digit. By the proof of Lemma 5, a number with significant digits inside that is not a multiple of lies below , and then and . Such a one-digit number could beat only if ; then contains and , so its width, which is at most , is at least , and . So is one of the subnormals with . Among them only has : there , , the output is at distance about from , and the other one-digit candidates and are farther.
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.