On this page
Type 0.1 + 0.2 into a programming language and you may get 0.30000000000000004 instead of 0.3. Nothing is broken. The site 0.30000000000000004.com shows the same result in many languages, because most use the same number format. Learn that format and you can avoid the errors that matter.
Why does 0.1 + 0.2 not equal 0.3 in a computer?
Computers store fractions in base 2, and most decimal fractions have no exact binary form. The Python tutorial on floating-point arithmetic shows that 1/10 in base 2 is the repeating fraction 0.000110011001100110011…, so any finite number of bits gives only an approximation. Python stores 0.1 as 3602879701896397 / 2⁵⁵, which is exactly 0.1000000000000000055511151231257827021181583404541015625 (Python docs).
It is the same trap as 1/3 = 0.333… in decimal. A fraction terminates in base 10 only when its denominator has no prime factors other than 2 and 5 (see how to spot a terminating decimal without dividing). In base 2 the only allowed prime is 2, and 10 = 2 × 5, so 1/10 repeats forever.
The stored 0.1 and 0.2 are each slightly too big. Their exact sum lands halfway between two neighbouring doubles, and the round-half-to-even rule picks the upper one, 0.30000000000000004. The stored 0.3 is the lower neighbour. So 0.1 + 0.2 == 0.3 is False.
How does an IEEE 754 double store a number?
A double-precision number (IEEE 754 binary64) has 64 bits: 1 sign bit, 11 exponent bits and a significand with 53 bits of precision, 52 of them stored (Wikipedia: double-precision format). The exponent carries a bias of 1023, and the significand's leading 1 is implied. The value is (−1)^sign × 1.mantissa × 2^(exponent − 1023).
For 0.1 (from Python's fraction above): sign 0, exponent field 1019 (power 2⁻⁴), and mantissa 1001 twelve times then 1010, rounded up. The 53-bit significand gives 15 to 17 significant decimal digits (Wikipedia: double-precision format). If converting fractions to binary is new to you, practise with decimal-to-binary conversion by hand.
What is machine epsilon?
Machine epsilon is the difference between 1 and the next larger floating-point number (Wikipedia: machine epsilon). For doubles it is 2⁻⁵² ≈ 2.22 × 10⁻¹⁶, the value Python's sys.float_info.epsilon reports (Python sys docs). Rounding to the nearest double makes a relative error of at most half that, 2⁻⁵³ ≈ 1.11 × 10⁻¹⁶ (Wikipedia: machine epsilon).
So each operation is accurate to about 16 digits. The danger is what a subtraction does to roundings already made.
What is catastrophic cancellation?
Catastrophic cancellation is when subtracting good approximations to two nearby numbers can give a very bad approximation to their difference (Wikipedia: catastrophic cancellation). David Goldberg's 1991 paper in ACM Computing Surveys, What every computer scientist should know about floating-point arithmetic, puts it this way: catastrophic cancellation "occurs when the operands are subject to rounding errors".
How do you rewrite the quadratic formula to avoid cancellation?
The quadratic formula article on Wikipedia notes that practical programs choose which formula to use for each root depending on the sign of b, and use x₁x₂ = c/a for the other root. Here is why, worked in double precision for x² + 10⁸x + 1 = 0, whose roots are close to −10⁸ and −10⁻⁸.
Discriminant: b² − 4ac = 10¹⁶ − 4. This even integer is stored exactly.
Square root: √(10¹⁶ − 4) ≈ 10⁸ − 2 × 10⁻⁸. Doubles near 10⁸ are 2⁻²⁶ ≈ 1.49 × 10⁻⁸ apart, so the computer gets 10⁸ − 2⁻²⁶, correctly rounded.
Naive small root: (−b + √D)/2 = (−10⁸ + 10⁸ − 2⁻²⁶)/2 = −2⁻²⁷ ≈ −7.45 × 10⁻⁹. The true root is about −1.0 × 10⁻⁸, so the answer is about 25% wrong with no correct digits.
Stable rewrite: find the large root first, where nothing cancels: x₁ = (−b − √D)/2 = −10⁸. Then x₂ = c/(a·x₁) = −1 × 10⁻⁸, correct to full precision.
As one formula: x = 2c/(−b ∓ √(b² − 4ac)), taking the sign that adds magnitudes. In Wikipedia's example, x² − 1634x + 2 = 0 on a ten-digit calculator, the usual formula gives the small root to four correct digits and 2/x₁ gives all ten. The textbook formula is still right on paper (refresher: quadratic equation formulas with worked examples). To practise quadratics, the Fermi Problem Library has a step-by-step solver that explains the reasoning.
How do you avoid floating-point rounding errors in practice?
Never compare floats with ==. Test whether |x − y| is below a small tolerance times the larger of |x| and |y|.
Before subtracting nearly equal numbers, try a rewrite: a conjugate, the product of roots, or (x + y)(x − y) for x² − y² (Wikipedia: catastrophic cancellation).
Expect 15 to 17 significant digits from a double, not more.
Keep money in whole paise as integers, not rupees as decimals.
If a formula subtracts two numbers that agree in their first few digits, rewrite it before trusting the answer.
If binary fractions or exponents still feel shaky, the Fermi Concept Library covers every concept in physics, chemistry, biology, mathematics and computing.



