Hello! Welcome back to our course.
In the last lesson, we saw a dramatic failure. The seemingly efficient single-pass "textbook" formula for variance produced a nonsensical negative result when calculating the variance of a dataset like {10⁹ + 4, 10⁹ + 7, ...}. In contrast, the slower two-pass algorithm worked perfectly. We mentioned that the culprit behind this failure is a phenomenon called catastrophic cancellation.
Today, we will put that phenomenon under the microscope. Our learning outcome is to explain the concept of catastrophic cancellation as a source of error in floating-point arithmetic. To do this, we'll first need to understand the fundamental reason why computers struggle with real numbers. We will then define catastrophic cancellation, analyze why it happens, and look at some classic examples to build a solid intuition for this critical concept in numerical computing.
1. Why Computers Can't Be Perfectly Precise
Before we can understand what goes wrong in a calculation, we must first understand how the calculation is performed. On a computer, real numbers are not stored with infinite precision. They are approximated using a system called floating-point arithmetic.
The core idea is to represent a number in a form of scientific notation, but in base-2: a significand (the significant digits) and an exponent. For example, a number might be stored as 1.01101... × 2⁵. However, computers have a finite number of bits (usually 32 or 64) to store both the significand and the exponent. This has two immediate consequences:
- Representation Error: Some decimal fractions that are simple for us (like 0.1) have an infinitely repeating representation in binary, much like 1/3 is
0.333...in decimal. Since the computer has to truncate this at some point, the stored value is only an approximation. - Accumulation of Error: When you perform calculations with these approximate numbers, the small errors can add up.
The following video provides an excellent introduction to this problem.
Lecture 5: Floats and Approximation Methods
This lecture from MIT OpenCourseWare demonstrates the core problem with a simple Python example and then explains the basics of how floating-point numbers are represented.
Please watch the following segments: The motivating example (00:11 - 01:41). How decimal fractions are converted to binary and why some are not exact (06:41 - 11:58). How numbers are stored as a significand and exponent (18:45 - 22:48). How small approximation errors accumulate (22:48 - 24:30). Focus on the core idea that finite storage leads to approximation, and these small approximations can lead to noticeable errors.
The key insight is that because of the finite number of bits in the significand, there are "gaps" between the numbers that a computer can represent. The size of these gaps gets larger as the magnitude of the numbers increases.
The following reading explores this concept of "spacing" with some practical Python examples.
This article, 'Floating-Point Arithmetic', provides a great hands-on feel for these concepts using Python. It introduces the np.nextafter function to show the spacing between numbers and demonstrates what happens when you try to add a number that is smaller than this spacing.
Please read the sections starting from 'One notable feature of floating-point numbers...' down to the end of the section 'Roundoff errors'. Pay special attention to the example of adding 1 billion to 1e25 and seeing no change. This is a crucial concept known as 'loss of significance' or 'swamping'.
The example where 1e25 + 1e9 == 1e25 is striking. The gap between 1e25 and the next representable number is so large (over 2 billion!) that adding 1 billion isn't enough to "cross the mountain peak" to the next value. This loss of information is a key type of floating-point error, but it is not catastrophic cancellation. Catastrophic cancellation is something more subtle and dangerous.
2. Catastrophic Cancellation: The Anatomy of a Disaster
Now we're ready to tackle the main subject.
Catastrophic cancellation is the extreme loss of precision that occurs when subtracting two nearly equal numbers.
Let's say we have two numbers, and , that are very close to each other. When the computer stores them, it introduces small representation errors. Let's denote the floating-point representations as fl(x) and fl(y).
fl(x) = x(1 + ε₁)fl(y) = y(1 + ε₂)
Here, and are the small relative errors from the initial representation. Now, what happens when the computer calculates fl(x) - fl(y)?
The result is x(1 + ε₁) - y(1 + ε₂) = (x - y) + (xε₁ - yε₂)
The true answer is (x - y). The error in the result is the term (xε₁ - yε₂). The problem arises when we look at the relative error:
If is very close to , the denominator (x - y) becomes very small. Even if the numerator (xε₁ - yε₂) is also small, dividing by a much smaller number can make the relative error enormous. The leading, correct significant digits cancel each other out, leaving you with a result dominated by the noise from the representation errors.
This is "catastrophic" because you can lose almost all of your correct significant digits in a single operation. The following resource provides a formal analysis and a clear example.
MATH 3795 Lecture 2. Floating Point Arithmetic
These lecture notes from the University of Connecticut provide a concise mathematical treatment of catastrophic cancellation. It walks through the error analysis and provides the formal condition for when cancellation occurs.
Please read the sections titled 'Floating Point Arithmetic: Cancellation'. Start with the 'Example' on page 13. Notice how rounding the two initial numbers leads to a huge relative error of ~70 in the final result. Follow the derivation on page 15 that leads to the relative error formula we just discussed. Note the summary on page 16, which explicitly states when catastrophic cancellation can occur.
This phenomenon is vividly illustrated in the following plot. It shows the error when computing sqrt(1+x) - sqrt(1-x). For very small x, sqrt(1+x) and sqrt(1-x) are nearly equal, creating a perfect scenario for catastrophic cancellation.

Test your understanding!
Imagine you are using a toy floating-point system that can only store 4 significant decimal digits. You want to compute 9.876 - 9.875.
- What are the values of
xandy? - In this toy system,
fl(x)andfl(y)are already represented perfectly. Now, computefl(x) - fl(y). - Now let's try to compute
9.876_123 - 9.875_987.- What are
fl(9.876_123)andfl(9.875_987)in your 4-digit system (use standard rounding)? - Compute the difference of these rounded numbers.
- Compare your computed difference to the true difference. What happened?
- What are
Show answer
x = 9.876,y = 9.875.fl(x) - fl(y) = 9.876 - 9.875 = 0.001. The result has only one significant digit, whereas the original numbers had four. We've lost 3 significant digits.- Let
x = 9.876123andy = 9.875987.- Rounding to 4 significant digits,
fl(x) = 9.876andfl(y) = 9.876. - The computed difference is
9.876 - 9.876 = 0.0. - The true difference is
9.876123 - 9.875987 = 0.000136. - What happened: All of the significant digits were lost! The initial rounding error, though small, was larger than the true difference between the numbers. After subtraction, the result is completely wrong due to catastrophic cancellation.
- Rounding to 4 significant digits,
3. Avoiding Catastrophe
The good news is that catastrophic cancellation is often a flaw in the formula, not an unavoidable law of physics. It can frequently be fixed by rewriting the expression in a way that avoids the subtraction of two nearly equal numbers.
Let's look at two classic examples of this.
MATH 3795 Lecture 2. Floating Point Arithmetic
The same UConn lecture notes demonstrate how to fix catastrophic cancellation in two common problems: calculating 1 - cos(x) for small x, and finding the roots of a quadratic equation.
Please read 'Example 1' (page 17-18) and 'Example 2' (page 20). For each example, focus on: Why the naive formula suffers from catastrophic cancellation. The alternative, stable formula that avoids the problem.
In both cases—calculating 1 - cos(x) and solving the quadratic equation—a simple algebraic reformulation turns a numerically unstable calculation into a stable one. This principle of reformulating expressions to avoid numerical pitfalls is a central theme in numerical analysis.
Conclusion
In this lesson, we have uncovered the hidden dangers of floating-point arithmetic.
Key Takeaways:
- Computers approximate real numbers using a finite-precision format (floating-point), leading to representation errors and non-uniform gaps between representable numbers.
- Catastrophic cancellation is a severe loss of precision that occurs when subtracting two nearly equal numbers.
- The subtraction cancels out the accurate, leading significant digits, leaving a result dominated by the initial, small representation errors.
- This is not a flaw in the computer's hardware but a vulnerability in mathematical expressions. It can often be fixed by reformulating the expression to avoid the problematic subtraction.
Preview of the Next Lesson:
Now you have the tool you need to diagnose the failure we saw in our first lesson. In the next lesson, we will analyze why the single-pass variance formula is numerically unstable. We'll see that the "textbook" formula, E[X²] - (E[X])², is a textbook case of catastrophic cancellation when the standard deviation is small relative to the mean, setting the stage perfectly for introducing Welford's algorithm as the solution.