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. is a positive binary64 number with , and its rounding interval has ends and with , , . Both ends belong to when is even and neither does when is odd. So and .
What is assumed. (A1) Line L3 computes the exact floors , , at the starting exponent . This is Adams's table-precision result. (A2) The flags of lines L4, L5 and L7 tell exactly whether and are integers, in every case where that changes the output. For this is exact: is an integer iff , and , are not multiples of 4, so for the ends are never integers at that scale. For the cutoff is taken from the reference implementation. The tests check (A1) and the final output in exact rationals.
For a scale , call an integer admissible at if .
Lemma 1 (shortest decimals). Let be the largest such that some integer is admissible at . Then the decimals in with the fewest significant digits are exactly the with admissible at .
Proof. is narrower than itself, so , and the leading decimal positions of numbers in differ by at most one. Write for the position of the leading digit of and for the position of its last nonzero digit, so has significant digits. Every has by maximality. If contains a power of ten , then and also , so , the multiples of in are among and all have one digit; one digit is the minimum. Otherwise every has the same , and the length is minimal exactly when , that is, when is a multiple of (a multiple of in is not a multiple of ).
Lemma 2 (the start is fine enough). At the start, , and either is an integer or .
Proof. For , and . For , , , and . In both cases , and when the subtracted indicator is 1, . The interval has length , so its interior contains a multiple of , which gives ; when the indicator is 1 the interior has length at least and contains a multiple of , so . When the indicator is 0, either , so , and is an integer, or , so , and is an integer.
Lemma 3 (the loops stop at ). Keep the invariant that at exponent , , , and is the largest integer with allowed (that is, , minus 1 if is an integer and is excluded). Line L5 establishes it, and dividing by 10 preserves it because , while for an excluded integer end is again the largest allowed integer below .
The integers in are exactly the integers admissible at that are strictly above . So holds iff some multiple of 10 lies in , that is, iff some integer strictly above is admissible at . The only admissible integer it misses is itself, when is included and is a multiple of . Line L6 runs while the first kind exists; when it stops, no admissible integer at any coarser scale lies strictly above (each would give one at ). Line L7 then continues exactly while the included end is a multiple of ; the flag tracks that is an integer, and tests the next digit. When both loops stop, no integer is admissible at and some is admissible at (by Lemma 2 at the start, and by the loop condition after each step). Hence .
Lemma 4 (rounding). At let . Line L6 records , the first digit of , and whether all later digits are zero. Then iff or ( and some later digit is nonzero), and iff and all later digits are zero. Lines L8 and L9 therefore choose exactly when , or and is odd, or is not admissible; this needs at least one digit to have been dropped, or to be an integer, which Lemma 2 guarantees.
The candidate chosen is admissible. If is not admissible, then (it is at most ), and since some integer is admissible and all admissible integers exceed , the least of them is . If is chosen by rounding but were not admissible, then , so , hence and ; then neither nor is admissible, and no other integer is (they lie outside ), contradicting .
Theorem. Under (A1) and (A2), RYU returns with and the admissible integer at nearest to , ties to even. By Lemma 1 this is the shortest decimal in and the closest to among the shortest. is not divisible by 10, since otherwise would be admissible at .
Proof. Lemmas 2 and 3 give ; Lemma 4 gives the choice of . The nearest admissible integer to is or , because these are the integers on either side of and whichever is nearer and admissible is chosen.