We have proved that SmallInteger friendly 58 bits arithmetic and 116 bits table for powers of 10 was enough to make Russ Cox's algorithm work for shortest decimal representation of Float. But The algorithm may also be used for printing a Float with a fixed number of significant digits and also for parsing a decimal representation back to Float binary representation. Unlike original work which can handle up to 18 digits with 64 bits arithmetic, the limited 58-116 bits precision only enable printing as well as parsing 17 significant digits. But we have to prove it. The strategy is exactly the same as for shortest decimal: enumerate the exponent range exhaustively, and for each exponent, build all the adversarial cases that may falsify the significant part of unrounded scale (uscale). The class side of RussCox58PrinterTest has been equipped for building those adversarial cases for both fixed width printing and decimal parsing up to 17 digits.
For the shortest decimal, it was possible to prove that uscale will always answer the correct value, thanks to the alwaysSticky that has been added to the algorithm. Unfortunately, we can show that this is neither the case for fixed width printing nor for decimal parsing: the exhaustive search exhibits exactly
- 1 case of Float that would make uscale answer an incorrect result for the original fixed width printing algorithm
- 1 case of decimal that would make uscale answer an incorrect result for the decimal parsing algortihm
Both cases happen for large power of ten $p$ and 17 digits. One case among all possible Float value, that's extremely few, but already too much. A wrong uscale does not necessarily imply a wrong result: the last two bits of uscale are there for obtaining a correct rounding direction. But sometimes an incorrect bit does not change the rounding. The three last bits count: o.ht
- o is integer part parity, the odd bit
- h is half bit representing 1/2
- t is the sticky bit representing the presence of non null truncated bits in the rest of exact result (variable $s$ is already taken for shift in uscale and would be confusing)
We have seen previously that the sticky bit is always correct thanks to alwaysSticky for large powers of ten $p$. It can also be proved correct for small powers $p$. So the problem only happens if limited precision of $10^p$ is causing a carry to propagate up to half bit h or above. We will see that it is exactly the case for the two failing uscale tests which necessarily happens for large $p$, for which $t=1$. There are thus only 4 possible cases.
- true result is 0.01 should round to 0 false result is 0.11 round to 1
- true result is 1.01 should round to 1 false result is 1.11 round to 10
- true result is 0.11 should round to 1 false result is 1.01 round to 1
- true result is 1.11 should round to 10 false result i 10.01 round to 10
Incorrect rounding happens when the carry caused by ceil approximation of $10^p$ transforms h from 0 to 1.
If the carry is transforming 1 to 0, then it is causing a premature round-up of integer part, but this round-up would have been performed anyway by the sender just after calling uscale.
Unfortunately, we can verify that the two failing cases are transforming h from 0 to 1. But fortunately, we will see that it is not a problem. Let's see.
The principle is the same as shortest decimal: we are searching for $x$ which cancels the truncated bits of product $x \cdot pm$ in limbs $l_3,l_2$, or IOW $(x \cdot pm) \bmod 2^{116-s} \lt 2^{58}$. For fixed width printing, only high 53 bits of $x$ is used when performing $x \cdot pm$ in uscale, so we search for $x$ multiple of $2^5$. We can find several such $x$ for various binary float exponents $e$, most only affect the sticky bit which we correct afterward anyway, except the one problematic case:
x:=16r3B5D0CE5FEE7080. p:=181. s:=2.
u:=(Pow116 fiveRaisedTo: p) uscaleX1: x>>29 x0: (x bitAnd: 16r1FFFFFFF) shift: s.
This happens for
lp := (p * 108853) bitShift: -15. e:=s-3-lp.
f:=x asFloat timesTwoPower: e.
That is when printing 1.6107261995513574e-164 and asking for 17 digits.
We can easily compute the expected value exactly:
exact := 10**p*(x timesTwoPower: e)*4.
utrue := exact floor bitOr: exact isInteger not asBit.
And verify u-utrue=2, approximated u is incorrect up to half bit.
This can look strange: shortest decimal prints 17 digits without failing, why would asking for 17 digits fail?
There are several differences, shortest evaluate at different $x$ taking interval bounds into account, with a different power of ten $p=180$ and a different shift $s=1$, not 2. With 116 bits we are at the frontier of correct approximation, a single bit more or less makes the difference. The problem here is that we obtain 18 digits, $u \ge 10^{17}$, with integer part 161072619955135735 and trailing ht bits .01; $p$ was over estimated because the base 10 exponent ((e + 57) * 78913) bitShift: -18 was underestimated. The algorithm in this case should reduce with a division by 10, and if we do apply the division by 10 from original algorithm which cares for both half and sticky bit, then the problem vanishes. My first workaround was to decrement $p$, adjust $s$ and restart the uscale. That costs a 2nd call in about 16% of cases. Claude did suggest a more efficient solution. There is at most a single power of 10 per binade. So, if in each binade, we tabulate the last $x$ being lower than the next power of ten in a class variable DecadeBoundaries, we can have an exact value of $p$ and avoid the second call. The main advantage is to avoid completely the failing case of uscale. Float exponent range is a bit more than 2000, so that means a table with a bit more than 2000 58 bits SmallInteger, problem solved at a reasonable cost. Or we can just use original careful division and avoid the table - classical memory versus runtime trade-offs.
For the parsing (10**17-1) highBit=57, so this time $x$ might just be a multiple of 2 in worst case. The search for adversarial cases must be performed for all possible values of $p$ and for all the possible high bit $k$ of digits. $k$ is in the interval from 1 to 57. The single problematic case that makes uscale fail is
x:=16r2E54091785646FC. p:=-215. s:=-2.
u:=(Pow116 fiveRaisedTo: p) uscaleX1: x>>29 x0: (x bitAnd: 16r1FFFFFFF) shift: s.
This happens when parsing '52160987818987967e-215' and was found with
p:=-215. k := 56. d := 52160987818987967. k = d highBit.
lp := (p * 108853) bitShift: -15.
e:=53 - lp - k min: 1074
x:=d bitShift: 58 - k. s:=3 + e + lp - (58 - k).
exact := 10**p*(d timesTwoPower: e)*4.
utrue := exact floor bitOr: exact isInteger not asBit.
u-utrue=2
$u_{true}/4$ finishes with bits 10.01, $u/4$ with bits 10.11, it is again a case that is causing incorrect rounding as announced above. But it is again a case of overestimated exponent, the binary exponent $e$ this time. Indeed $\lfloor u/4 \rfloor$ has 54 bits instead of 53, so $u$ must be shifted 1 bit to the right. A correctly rounded right-shift does produce 1.01 in both cases, and so the error does not propagate to the reconstructed Float. We can call this lucky, or just like printing, say that a correct estimation of exponent would completely avoid the problem. What is lucky is that 116 bits is just at the edge of correctness. The adjunction of alwaysSticky flag in Pow116 was necessary to obtain this result, and this adjunction of this flag in the precomputed table of powers of ten is enough to make the whole algorithm work, finally a very small deviation from the original work.
We now have a fast algorithm proven correct for printing shortest decimal, but also for printing up to 17 decimal digits, or parsing up to 17 decimal digits, which covers most common usages. We can eventually start integrating the algorithm in Squeak trunk. This is important for letting Squeak and other dialects using OpenSmalltalk VM exchange large data sets efficiently without having to resort to binary formats.