Printing Floating Numbers is Surprisingly Hard!
summary
Computers represent decimals in binary so 0.1 + 0.2 ≠ 0.3 when printed; IEEE 754 maps many decimals to a single binary value and printing must pick a shortest decimal that roundtrips. The original Dragon4 (1990) is provably correct but uses big‑integer math and is slow. Grisu3 (2013) uses 64‑bit machine integers, is correct about 99.5% of the time and falls back to Dragon4 otherwise, running ~12.5× faster. Ryu (recent) uses only machine integers, skips digits in bulk, and runs ~3× faster than Grisu3; all focus on output, not input.
AI-written summary · about
chapters (7)
- 0:00Introduction
- 1:08Why 0.1 + 0.2 ≠ 0.3
- 3:35Floating point basics
- 4:37Dragon4 algorithm explained
- 8:13Grisu3 vs Dragon4
- 11:05Ryu algorithm overview
- 13:13Open challenges
related talks
11mPrinting floating point numbers is surprisingly hard!!
01h 41mFast Conversion From Cpp Floating Point Numbers
01mTalk Preview: A new dragon in the den: fast conversion from floating-point numbers
57mFloating-Point to String Conversion in C++: Exploring Algorithms & Performance
58mPortable floating-point calculations
01h 00mFloating-Point <charconv>: Making Your Code 10x Faster With C++17's Final Boss
16mWhat About the Integer Numbers?
26mHow Go Avoided the Integer Promotion Footgun
