×
About Floating Point
Type 0.1 + 0.2 into almost any programming language and it says 0.30000000000000004. Nothing is broken: the computer keeps numbers in binary, and in binary 0.1 never ends (0.000110011001100…), just as 1/3 never ends in decimal. So it keeps the nearest number it can hold, a tiny bit off, and the tiny bits add up. This page lets you see exactly what is stored, digit for digit.
BITS : type a number (or a sum such as 1/3 , 2^53+1 or 2^-1074 , or -0 , inf , nan ). It is rounded into the three standard sizes of float: half (16 bits), single (32) and double (64, what JavaScript and most spreadsheets use). Each bit is a switch; click one to flip it. Pick a size on the left and the sheet below shows the value those bits really hold, written out in full (every float has a decimal that ends), how far that is from what you typed, how the sign, exponent and fraction make it, and the floats either side. The keys [ and ] step to the next float down and up.
THE NUMBER LINE : a toy float of just 8 bits (1 sign, 4 exponent, 3 fraction) has only 120 values from 0 up to 240, so you can see them all. Each power of two up, the gap between them doubles. Drag the line to slide it, scroll or use the buttons to zoom. Below, the same thing for the real sizes: slide to any power of two and see the floats either side. At 2,048 a half float can no longer hold every whole number; a single gives up at 16,777,216 (224 ) and a double at 9,007,199,254,740,992 (253 ), which is why JavaScript calls 253 − 1 its largest safe integer.
SUMS GONE WRONG : four sums, each with the stored values written out exactly. A + B and A − B work out as you type. ADDING OVER AND OVER and BIG AND SMALL run when you press the button; change a setting and the old answer fades until you run it again.
WHEN IT MATTERED : two well documented failures. Slide the hours the Patriot battery had been running, or the value Ariane 5's software tried to squeeze into 16 bits.
THE STORY
Early computers each did fractions their own way, and the same program could give different answers on different machines. In the late 1970s the Institute of Electrical and Electronics Engineers set up a committee to agree a standard. William Kahan of the University of California, Berkeley, who was also advising Intel on its 8087 maths coprocessor chip (1980), wrote the proposal with Jerome Coonen and Harold Stone that became the heart of IEEE 754, published in 1985. It fixed the layouts used here, rounding to the nearest value (ties to the even one), infinities, NaN ("not a number", for things like 0/0), minus zero, and the "subnormal" numbers that fill the gap just above zero. Kahan received the Turing Award in 1989, chiefly for his work on floating point. The 2008 revision added the 16-bit half size. Nearly every computer, phone and graphics chip now follows it, so 0.1 + 0.2 gives the same wrong answer everywhere.
The summing trick on the SUMS tab, which carries the lost low digits along in a second number, is Kahan's too, published in 1965.
The Patriot clock. On 25 February 1991, during the Gulf War, a Patriot missile battery at Dhahran in Saudi Arabia failed to track an incoming Scud, which hit an American army barracks and killed 28 soldiers. The US General Accounting Office's report (GAO/IMTEC-92-26, 1992) explains why. The system counted time in tenths of a second, and to turn the count into seconds it multiplied by 1/10 held in a 24-bit fixed-point register. In binary 1/10 never ends, so it was chopped. The GAO gives the error as about 0.000000095 seconds per tenth, which is what you get by keeping 23 bits after the binary point, so that is what the page does. The battery had been running for about 100 hours, by then the clock was about 0.34 seconds out, and a Scud travels at about 1,676 metres a second, so more than half a kilometre in that time. The radar looks for a target in a "range gate", a window placed where the target should be next; with the clock wrong the window was in the wrong place, the Scud wasn't in it, and the detection was dropped. The Army had already sent a warning that very long running times could cause problems, and corrected software reached Dhahran the next day. This was fixed-point arithmetic, not floating point, but the root is the same one this page is about: a binary fraction cannot hold 1/10 exactly.
Ariane 5 flight 501. On 4 June 1996 the first Ariane 5 veered off course and broke up about 40 seconds into its flight sequence (some half a minute after lift-off). The inquiry board, chaired by Jacques-Louis Lions, found that software in the inertial reference system, carried over from Ariane 4, converted a 64-bit floating-point number related to the rocket's horizontal velocity into a 16-bit signed integer, which can hold only −32,768 to 32,767. Ariane 5 flies a different early path from Ariane 4, with much higher horizontal velocity, and the number was too big. The conversion was not protected, the error stopped the unit, and the backup unit, running the same software, had stopped the same way a moment before. The flight computer then read diagnostic data as if it were flight data, swung the engine nozzles hard over, and the rocket broke up. The function that failed did no useful work after lift-off.
HOW IT WORKS
A float is a sign, an exponent and a fraction: value = ± 1.fraction × 2exponent − bias , where the bias is 15, 127 or 1023. With the exponent bits all 0 the leading 1 becomes 0 (a subnormal number, getting gradually smaller down to zero); all 1 means infinity (fraction 0) or NaN (anything else). Because each value is a whole number times a power of two, its decimal always ends; the page keeps every value as an exact fraction of whole numbers in JavaScript's BigInt, rounds what you type straight into each size (to the nearest, ties to even), and writes every digit. The bit patterns are checked against the computer's own (JavaScript's DataView, Float32Array and Math.fround). The SUMS answers are the computer's own arithmetic: double sums are JavaScript's, single sums use Math.fround after each step.
The Patriot sums: ticks = hours × 36,000; error per tick = 1/10 − (1/10 chopped to 23 binary places) = 1/10,485,760 of a second, exactly; drift = ticks × that. The page's drawing shows how far the Scud moved in that time; the real size of the range gate isn't in the public report, so it isn't drawn.
Simplifications: the half and single sizes follow the same rules as the double; NaN's spare bits (its "payload") are shown but not explained further; the toy 8-bit float is made up for the picture (several such teaching formats exist).
FURTHER WATCHING AND READING