首页›research!rsc›文章

Pulling a New Proof from Knuth’s Fixed-Point Printer

Russ Cox · 2026年1月10日

AI 摘要 AI 生成,可能有误

本文是对唐纳德·克努特1989年一篇论文的回应,该论文证明了一个将16位定点二进制小数转换为其最短精确十进制表示形式的程序的正确性,谨以此文庆祝克努特的88岁寿辰。作者从一个更简单的程序入手——该程序用类 APL 的 Ivy 语言实现,其正确性可以平凡地直接证明——然后逐步将其变换为 P2,并证明每一步变换都是正确的。作者认为这为克努特的程序 P2 提供了一个更好的证明。这篇文章还探讨了几种替代解法,并在结尾处反思了语言和记法如何塑造程序与证明。

Introduction

Donald Knuth wrote his 1989 paper “A Simple Program Whose Proof Isn’t” as part of a tribute to Edsger Dijkstra on the occasion of Dijkstra’s 60th birthday. Today’s post is a reply to Knuth’s paper on the occasion of Knuth’s 88th birthday.

In his paper, Knuth posed the problem of converting 16-bit fixed-point binary fractions to decimal fractions, aiming for the shortest decimal that converts back to the original 16-bit binary fraction. Knuth gives a program named P2 that leaves digits in the array d and a digit count in k:

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 research!rsc

Fast Unrounded Scaling: Proof by Ivy

AI 摘要This post proves the correctness of uscale, a fast unrounded scaling algorithm based on a 64×128-bit multiplication and floating-point approximation, which underlies the author's floating-point printing and parsing algorithms (FixedWidth, Short, and Parse). The proof proceeds through a series of lemmas handling exact results, small positive and negative powers, and finally the large-power cases, which cannot be exhaustively tested directly. For those large-power cases, the author uses the Ivy calculator—an APL-inspired language with arbitrary-precision rationals and right-to-left precedence—to implement a two-step approach, loosely following work by Vern Paxson and Tim Peters, that solves a 'modular search' problem to find the first satisfying value and a 'modular minimum' problem to verify all relevant inputs.

Floating-Point Printing and Parsing Can Be Simple And Fast

AI 摘要This article presents fast floating-point printing and parsing algorithms built on a new primitive called fast unrounded scaling, which approximates scaling by powers of ten, often in a single 64-bit multiplication. These simple algorithms reportedly outperform all prior known methods, including Dragon4, Grisu3, Ryū, Schubfach, and Dragonbox, and the author plans to include a Go implementation in Go 1.27. The post walks through the necessary concepts (fixed-point and floating-point numbers, unrounded numbers) and outlines implementations for fixed-width and shortest-width printing as well as decimal parsing.

Floating Point Formatting

AI 摘要The article introduces the 'Floating Point Formatting' blog series, which began in 2011 and continued with new posts in 2026. It lists four posts covering topics such as floating-point to decimal conversion, a proof inspired by Knuth's fixed-point printer, simple and fast floating-point printing and parsing, and a proof of fast unrounded scaling verified with Ivy.

Differential Coverage for Debugging

AI 摘要The article describes 'differential coverage', a debugging technique that compares the code coverage profile of a failing test with those of passing tests to isolate code blocks unique to the failing case. Demonstrating in Go, the author diffs two coverage profiles and views the result with go tool cover -html, successfully narrowing an injected bug down to 10 lines among over 15,000. While not foolproof (e.g., data-dependent bugs or insensitive tests can evade it), the technique is cheap to compute and can save considerable debugging time.

Hash-Based Bisect Debugging in Compilers and Runtimes

AI 摘要The article traces the history of debugging with binary search, from bisecting data (such as finding one bad punched card among a thousand) to bisecting version history, which was popularized by tools like git bisect that automatically pinpoint the commit that introduced a bug. It then introduces a new trick for debugging in a different dimension: hash-based bisecting of program locations, which selectively enables or disables subsets of program lines, call stacks, or code sequences to identify exactly which code causes a failure. Illustrated with a Go toolchain example, this technique is presented as a 'magic wand' for debugging large, opaque test failures in unfamiliar code bases, compilers, and runtimes.