Why 0.1 + 0.2 isn't 0.3: flip the 64 bits a computer uses for decimals

30seconds
Type 0.1 + 0.2 into almost any programming language and you get 0.30000000000000004. Nothing is broken. Computers count in powers of two, and a tenth is an endless repeating fraction in binary, so it has to be rounded to fit in 64 bits.
Flip the real bits of a double yourself and see the exact value they stand for, and learn how a rounding error this small helped cause a deadly missile failure in 1991.
Deep dive · 3 min
More in Computing
View all →
Why one extra disc doubles the work: the Tower of Hanoi and 2ⁿ − 1
Move a stack of discs between three pegs, never putting a big disc on a small one. It sounds easy, and with 3 discs it is. But every extra disc doubles the…
Why sorting gets slow so fast: bubble sort versus merge sort and n log n
Two sorting methods can both be perfectly correct and still differ by a factor of 25,000 on a big list. Race bubble sort against merge sort on the same…
How your phone finds the quickest route: Dijkstra's shortest-path algorithm
Your phone finds the fastest route across millions of roads without trying them all. The trick is a rule Edsger Dijkstra devised in 1956: always finish the…
Why eight yes/no answers can draw a fractal, make noise or compute: elementary cellular automata
Take a row of lit and dark squares and repeat one tiny rule to make the next row: look at a square and its two neighbours, then decide if it is lit. There are…
Why one flipped bit can be found and fixed: Hamming's error-correcting code
A single flipped bit can turn a good file into garbage, yet a machine can find and repair it without ever seeing the original. In 1950 Richard Hamming, tired…
Why swapping cards at random is not a fair shuffle: Fisher–Yates and the 27 paths
Shuffling three cards by swapping each one with a random card from the whole row looks fair, but it is not. It makes 3 × 3 × 3 = 27 equally likely paths for…