Knuth's Algorithm D Had a Bug Since 1995, but Only in Odd Bases
Software / news
Knuth's Algorithm D Had a Bug Since 1995, but Only in Odd Bases
A 1995 errata change left Theorem B unproven for the uncapped trial quotient, and LLVM's APInt carries the book's old typo harmlessly.

A proof in Donald Knuth's long-division algorithm stopped matching the algorithm after a 1995 revision, and it took until 2026 for anyone to say so. A developer who goes by Kolja found that step D3 of Algorithm D can leave the trial quotient two digits wide, and that the following step then subtracts the wrong amount. Knuth accepted the correction on May 14, according to Kolja's write-up.
The bug cannot occur on any modern processor. It needs an odd number base, and hardware divides in base 2^32 or 2^64.
What went wrong in step D3 of Algorithm D
Algorithm D is the schoolbook method for dividing one multi-limb integer by another, printed as Algorithm 4.3.1D in Volume II of The Art of Computer Programming. Each pass guesses a trial quotient, called q̂, from the leading limbs (the machine-word-sized digits of the number) and then corrects the guess downward in step D3.
Kolja was writing a multiprecision library when the proof of Theorem B, which bounds the guess error, looked unnatural. The cause, in Kolja's words: "the 1995 change swapped the q^x86 trial quotient for q^arm, but it did not adjust Theorems A" and B. The older text capped the guess at b minus 1, where b is the base. The 1995 errata removed the cap, and the theorems kept assuming it.
With no cap, D3's two corrections can finish with q̂ still two limbs long. Step D4 multiplies the divisor by only the low limb of q̂, so the subtraction is wrong and the error carries forward.
The base-3 example that breaks it
Kolja's smallest case is base 3. Dividing u = 45 by v = 16 should give 2. The trial quotient starts at 5, which is (1,2) in base 3. After both D3 corrections it is 3, written (1,0), still two limbs. The multiplication in D4 sees only the low limb, which is zero, so nothing is subtracted.
The failure needs q̂ to equal the true quotient plus three, which is b + 2, and that only happens when four conditions hold at once.
| Condition | Required value |
|---|---|
| Base b | odd |
| Second divisor limb v(n-1) | (b-1)/2 |
| Leading dividend limb u(n) | (b-1)/2 |
| Next dividend limb u(n-1) | b-1 |
Base 3 is not a base Knuth's MIX computer uses, Kolja wrote, but a counterexample exists for b = 65.
Who is affected: LLVM's APInt and two other cases
Kolja's post says b = 2^64 machines are unaffected because the error "requires an odd base". It lists Stefan Kanthak's analysed implementations as safe too, since each uses a while loop in D3 where the book prints two fixed corrections.
LLVM's APInt.cpp is the interesting case. Its KnuthDiv function tests qp == b in D3, a typo carried over from the third edition, which printed "test if q̂ = b". The corrected text says greater than or equal. Because LLVM works in base 2^32, which is even, the answers are identical either way.
LLVM contributors still want the code to match the corrected text. A pull request, #215695, was opened on Aug. 11 and was still open when this article was written. It is labelled a non-functional change and says it "stops it from relying on a property that is true but not proven". LLVM is also the back end of the Go compiler in our Marmot report.
- True quotient2
- Trial quotient after D3 corrections3
- Initial trial quotient5
Source: Kolja, A long division story, kolja.rs, accessed 2026-11-30
How the fix reached Knuth
Kolja reported the error and Knuth accepted it, with the correction filed on May 14, 2026. Knuth's TAOCP page lists the Volume II errata as last updated June 9, 2026. Kolja says the fix will appear in the 53rd printing and that the revised Theorem B "proves a different bound"; Knuth's page does not mention a 53rd printing, so that detail rests on Kolja alone.
Kolja received a hexadecimal dollar from the Bank of San Serriffe and handwritten notes, in which Knuth wrote: "I'm especially glad to have this correction."
The report came through the channel Knuth has since closed: his Moratorium page says error reports from LLM-assisted readers now arrive faster than he can read them. Kolja says Claude did not find this bug until told to examine the size of q̂ between D3 and D4.
The next milestone is the 53rd printing of Volume II, which Kolja says will carry the revised theorem.
Sources
More in Software
- 01Superpowers, at 330,000 Stars, Retunes Brainstorming a Third TimeIssue #2530 says the v7 questions waste tokens, and the fix in draft pull request #2756 was written by Claude Fable 5.2.
- 02Go 1.28 Turns Transport.CancelRequest Into a No-Op and Adds sync.CounterOf 25 accepted proposals in the Go 1.28 milestone, 17 are closed, and the CancelRequest GODEBUG is flagged as a release blocker.
- 03Unkernel Boots Linux 7.3.4 to Alpine in 0.3 Seconds on NodePooya Parsa's Vercel-sponsored project needs JSPI in the browser, has no IPv6, and ends the session on any kernel WebAssembly trap.
- 04Marmot, a Go Compiler on LLVM, Counts Its Benchmarks Two WaysThe experimental compiler's own pages put its M3 Pro run at 45 programs in one place and 43 in another, with 1.54x as the headline.