Review: The Art of Computer Programming, Volume 2: Seminumerical Algorithms¶
Citation
- Knuth, D. E. (1997). The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd ed. Addison-Wesley.
Parts of the book covered
The volume has two chapters, on random numbers and on arithmetic. This review covers two parts of the chapter on arithmetic. The first is Section 4.2.2, "Accuracy of Floating Point Arithmetic", pages 229 to 236: its opening, and Theorems A, B and C, which give the exact error of a floating-point sum. The second is the opening of Section 4.6.3, "Evaluation of Powers", pages 461 to 463, with Algorithm A. Page numbers are the book's own. In the PDF in this folder, the PDF page is the book page plus 12.
The associative law¶
The section opens with the problem that Hifuku's WGSL kernels meet: "Floating point computation is by nature inexact, and programmers can easily misuse it so that the computed answers consist almost entirely of 'noise'" (p. 229). Knuth notes that "a substantial loss of accuracy is expected from such additions and subtractions, but not from multiplications and divisions", and that one consequence of this "unreliability of floating point addition is that the associative law breaks down":
with a worked example in eight-digit decimal arithmetic (p. 229, equation 1). Here \(\oplus\) and \(\ominus\) are floating-point addition and subtraction.
The exact error of a sum¶
The section then turns to "exact relations that are satisfied by the floating point operations" (p. 235). Theorem A states that, for normalized floating-point numbers \(u\) and \(v\), and "provided that no exponent overflow or underflow occurs",
(p. 235, equation 40). With \(u' = (u \oplus v) \ominus v\) and \(v'' = (u \oplus v) \ominus u'\), Theorem B gives the error of the sum as an explicit formula (p. 236, equation 45):
The right side uses six floating-point operations and no comparison. Knuth writes that it gives "an explicit formula for the difference between \(u + v\) and \(u \oplus v\), in terms of quantities that can be calculated directly using five operations of floating point arithmetic" (p. 236). This is the algorithm that later literature calls 2Sum, or TwoSum.
Theorem C shortens it for radix 2 or 3 when the operands are ordered by magnitude: if \(b \le 3\) and \(|u| \ge |v|\), then (p. 236, equation 47)
The error then needs two floating-point operations and one comparison of absolute values. This is FastTwoSum, which Dekker (1971) gives under a condition on the exponents.
Knuth adds that "the proofs of Theorems A, B, and C do not rely on the precise definitions of round(x) in the ambiguous cases when x is exactly midway between consecutive floating point numbers; any way of resolving the ambiguity will suffice for the validity of everything we have proved so far" (p. 236).
Powers by the binary method¶
Section 4.6.3 opens with "the interesting problem of computing \(x^n\) efficiently, given \(x\) and \(n\), where \(n\) is a positive integer" (p. 461). The binary method squares the partial result for each binary digit of \(n\), and multiplies by \(x\) for each digit 1. Knuth notes that programs prefer to scan the digits from right to left, "because the available operations of division by 2 and remainder mod 2 will deduce the binary representation from right to left" (p. 461), and gives that form as Algorithm A (p. 462):
- A1: set \(N \leftarrow n\), \(Y \leftarrow 1\), \(Z \leftarrow x\).
- A2: at this point \(x^n = Y Z^N\). Set \(N \leftarrow \lfloor N/2 \rfloor\). If \(N\) was even, go to A5.
- A3: set \(Y \leftarrow Z \times Y\).
- A4: if \(N = 0\), stop, with \(Y\) as the answer.
- A5: set \(Z \leftarrow Z \times Z\), and go to A2.
The algorithm holds when "\(x\) belongs to any algebraic system in which an associative multiplication, with identity element 1, has been defined" (p. 462). It takes \(\lfloor \lg n \rfloor + \nu(n)\) multiplications, where \(\nu(n)\) is the number of ones in the binary representation of \(n\), one more than the left-to-right method "due to the fact that the first execution of step A3 is simply a multiplication by unity" (p. 463). Knuth adds that binary methods "are most applicable when the time to multiply \(x^j \cdot x^k\) is essentially independent of \(j\) and \(k\) (for example, when we are doing floating point multiplication ...)" (p. 463).
Relevance to Hifuku¶
Hifuku's WGSL kernels carry the quantities that need more than float32 precision as float-float values, which rest on the exact error of a sum. Three points from Section 4.2.2 bear on them.
- Theorem C is the form the kernels use by default. The library orders the operands by magnitude with a branch, \(|a| \ge |b|\), which is the hypothesis of Theorem C, and then forms the sum and its error in three operations.
- The tie-breaking rule does not matter. The WGSL specification does not name a tie-breaking rule for rounding to nearest. By the remark on page 236, Theorems B and C hold under any rule.
- The associative law is the weak point. Theorems B and C hold for the floating-point operations in the order written. The WGSL specification lets a compiler reassociate floating-point operations (section 15.7.5), and over the real numbers the error term of both theorems is zero. A compiler that regroups the operations therefore removes the error. This is why the WGSL library either hides each intermediate from the compiler or computes the error in integers.
Knuth excludes exponent overflow and underflow. The library's tests include subnormal inputs, so for those inputs the tests, not this theorem, establish that the error is exact.
Section 4.6.3 bears on the WGSL product kernel, which raises each site likelihood to the weight of its pattern.
- The kernel uses Algorithm A. Each invocation halves the weight, multiplies the accumulated product by the current power when the bit is 1, and squares the power. A weight of zero leaves the product at 1, where Algorithm A asks for a positive \(n\).
- Float-float multiplication is not associative. Algorithm A assumes an
associative multiplication, and its correctness argument does not bound
rounding error. The error bound of the kernel is derived separately, in
docs/theory/algebra/joldes2017tight-higham2002accuracy-weighted-product.py: the rounding errors of all the multiplications reach the result with multiplicities that sum to the weight.