Technology & Computing · Software & algorithms
Why a computer can't write down one tenth
Trace 0.1 into binary and back, and see why adding it three times doesn't give 0.3, why rounding first doesn't help, and what to do instead.
What you will learn
- Explain why most decimal fractions can't be stored exactly in binary
- Say what a machine actually stores when a program is given the value 0.1
- Predict the result of an equality test between an accumulated float and a literal
- Choose a comparison that works with approximate values
Lesson 1 of 4 · 5 min
Why a computer can't store 0.1 exactly
On February 25, 1991, a Patriot missile battery in Dhahran, Saudi Arabia, failed to track an incoming Scud missile. The Scud hit an Army barracks and killed 28 Americans. Investigators traced the failure to the way the battery's computer handled one small number, one tenth.
Computers store fractions in binary, which works like decimal with a different base. The decimal 0.625 means 6 tenths, 2 hundredths and 5 thousandths. The binary 0.101 means one half, no quarters and one eighth. Those are the same number written two ways.
Most decimals aren't that lucky. You know the problem from one third. In decimal you can write 0.3, then 0.33, then 0.333, and you never get there. Whether a fraction ends depends on the base.
Binary can only finish a fraction built from halves, quarters, eighths and so on. One tenth isn't. In binary it's 0.0 followed by 0011 again and again, forever. A computer has a fixed number of binary digits, called bits, so it has to stop somewhere.
What the computer keeps is the nearest binary fraction it can hold. Numbers stored this way are called floating-point numbers, or floats. On nearly every machine today, a float keeps the first 53 bits. For one tenth, the stored value is exactly 0.1000000000000000055511151231257827021181583404541015625.
That's a little more than one tenth. You get the same value on nearly every phone and laptop, because they share one standard format.
You rarely see it, because programs round what they display. Python prints the shortest decimal that leads back to the same stored value, and for this value that's 0.1. So you type 0.1, the machine stores the long number, and the screen shows 0.1 again.
None of this is a bug in the program or the language. It happens in every language that uses the computer's built-in arithmetic, because the limit is in binary itself.
Try to recallA program is given the literal 0.1 and later prints it as 0.1. What's actually stored, and why doesn't the display show it?
What's stored is the nearest binary fraction the computer can hold. One tenth repeats forever in binary and only the first 53 bits are kept, so the stored value is slightly more than one tenth. The screen still shows 0.1 because programs round what they display. Python prints the shortest decimal that leads back to the same stored value, and that's 0.1.
Back to Dhahran. The Patriot's clock counted time in tenths of a second. To turn the count into seconds, the computer multiplied it by its stored one tenth, which had been cut off after 24 bits.
Each tenth came up short by about a ten-millionth of a second. The battery had been running for more than 100 hours, and by then its clock was off by about a third of a second. A Scud covers more than half a kilometer in that time, so the system looked in the wrong place.
Today's computers keep 53 bits where the Patriot kept 24, so their one tenth is far closer. It still isn't one tenth.
Lessons in this topic
Why a computer can't store 0.1 exactly
One tenth repeats forever in binary, so a computer stores a near miss. See what it stores, why you rarely notice, and what happened when a missile battery relied on it.
Why 0.1 + 0.1 + 0.1 isn't 0.3 on a computer
Three slightly wrong tenths add up to a slightly wrong total, and an exact comparison notices. See why rounding first doesn't help and what to compare with.
Inside a 64-bit number, and where counting breaks
A double splits its 64 bits into a sign, an exponent and a mantissa. The mantissa's size sets the point, near 9 quadrillion, where whole numbers stop being exact.
Why floats suit measurements but not money
Floats are right for measured quantities and wrong when the decimal digits themselves must be exact, as with money. See the exact alternatives and how to look at what a float really holds.
Three quick stories from this topic
Three tenths that aren't
A computer adds 0.1 three times and gets 0.30000000000000004, which it says isn't 0.3
Each 0.1 is stored as a binary fraction a hair above one tenth, so the three small errors add up. The stored 0.3 is a different near miss. Rounding the inputs first doesn't help, because they're already as close as the format allows.
Rounding first doesn't help
Round each value to one decimal place and the comparison is still False. The values are already as close to their decimals as the format allows.
Test for closeness instead
Python's math.isclose reports that sum and 0.3 as close. For adding many values, sum uses extended precision, and math.fsum rounds only once.
The Patriot's one tenth
A Patriot missile battery failed in 1991 because its computer couldn't store one tenth
Its clock counted tenths of a second, and one tenth repeats forever in binary, so the stored value was cut short. After more than 100 hours of running, the error had grown to about a third of a second. The Scud it missed killed 28 American soldiers.
Why one tenth won't fit
In binary, one tenth is 0.000110011 with 0011 repeating forever, the way one third is 0.333 forever in decimal. Any computer has to cut it off somewhere.
A third of a second
A Scud covers more than half a kilometer in that time, so the battery looked in the wrong place. Corrected software arrived in Dhahran the next day.
Where counting by ones stops
In JavaScript, 9,007,199,254,740,992 plus 1 equals 9,007,199,254,740,992
A JavaScript number keeps 53 binary digits of precision. Past 2 to the 53rd power, about 9 quadrillion, the values it can hold are 2 apart, so adding 1 lands back on the same number. X sends its post IDs as text for this reason.
Sixty-four bits, three jobs
One bit holds the sign, 11 hold the exponent and 52 hold the digits. A leading 1 that's never stored brings the precision to 53 bits.
Why big IDs travel as text
X's documentation shows the ID 10765432100123456789 coming out of JavaScript as 10765432100123458000. Sent as text, every digit survives.
Sources
- 15. Floating-Point Arithmetic: Issues and Limitations - The Python Tutorial, Python Software Foundation
- Patriot Missile Defense: Software Problem Led to System Failure at Dhahran, Saudi Arabia (GAO/IMTEC-92-26), U.S. General Accounting Office
- The Patriot Missile Failure, Douglas N. Arnold, University of Minnesota
- Number - JavaScript | MDN, Mozilla Foundation
- X IDs, X Developer Platform





