Adding Floating-Point Decimals for Fun and Profit
Many people know that you shouldn’t do decimal calculations, such as those involving U.S. dollars and cents, with the floating-point numbers in most programming languages. This is because decimal numbers can’t be expressed exactly as such floating-point numbers, so you will encounter rounding errors. Famously, with IEEE double-precision floats, 0.1 + 0.2 is not 0.3, but rather, 0.30000000000000004. On the other hand, I use a Python REPL to add up decimal numbers for receipts all the time. Of course, my stakes are lower; I’m summing things in the $1–$100 range and know to manually round the microscopic errors off before copying the sum somewhere. But actually it’s quite often that those errors don’t appear at all. If we sum every pair of multiples of 0.01 up to 1.00 and look at whether the printed result is too large (blue), too small (red), or correct, we get a cool pattern: Figure 1: How floating-point affects summing two multiples of 0.01, up to 1.00 Where does this pattern come from?
2026-08-30 (2527 words) filed under Math, CS Many people know that you shouldn’t do decimal calculations, such as those involving U.S. dollars and cents, with the floating-point numbers in most programming languages. This is because decimal numbers can’t be expressed exactly as such floating-point numbers, so you will encounter rounding errors. Famously, with IEEE double-precision floats, 0.1 + 0.2 is not 0.3, but rather, 0.30000000000000004. On the other hand, I use a Python REPL to add up decimal numbers for receipts all the time. Of course, my stakes are lower; I’m summing things in…
saved by
related reading
- Floating point from scratch: Hard Mode · Tales on the wireessenceia.github.io
- There are Only Four Billion Floats-So Test Them All! | Random ASCII – tech blog of Bruce Dawsonrandomascii.wordpress.com
- Python Gotcha: Comparisons - Andrew Wegnerandrewwegner.com
- Making Deep Learning go Brrrr From First Principleshorace.io
- 1. Introduction — Floating Point and IEEE 754 13.3 documentationdocs.nvidia.com
- Transformer Inference Arithmetic | kipply's blogkipp.ly
- Your Softmax Is Broken. Here's the Fix.youtube.com
- 練習 - 探索浮點類型 - Training | Microsoft Learnlearn.microsoft.com
- All the Transformer Math You Need to Know | How To Scale Your Modeljax-ml.github.io
- log is non-monotonic in PHP and Luapurplesyringa.moe
- Writing Toy Software Is A Joyblog.jsbarretto.com
- Implementing High-Precision Decimal Arithmetic with CUDA int128 | NVIDIA Technical Blogdeveloper.nvidia.com