Catastrophic Cancellation
Numerical Methods
David Mayerich
Scalable Tissue Imaging and Modeling (STIM) Laboratory
Department of Electrical and Computer Engineering
Cullen College of Engineering
University of Houston
David Mayerich
STIM Laboratory, University of Houston

Loss of Precision
Losing Digits to Cancellation
Loss of Precision Theorem
David Mayerich
STIM Laboratory, University of Houston
Catastrophic Cancellation
-
Consider a floating point representation with a 5-digit mantissa:
David Mayerich
STIM Laboratory, University of Houston
-
Apply a simple addition and subtraction:
round
chop
Catastrophic Cancellation
-
This subtraction effectively reduced our numerical precision:
David Mayerich
STIM Laboratory, University of Houston
-
Each operation maintains the appropriate precision
-
However, we ended up with far less precision than we expect:
-
The result \(x=0.2\) has one digit of precision
-
The correct value \(x=0.151\) requires two digits of precision
-
We have \(5\) digits of precision, expect \(3\), but only get \(2\)
-
-
This is known as catastrophic cancellation
creating a relative error of
Loss of Precision Theorem
David Mayerich
STIM Laboratory, University of Houston
Let \(x\) and \(y\) be normalized floating-point numbers such that \(x > y > 0\). For a base \(b\) value, we can find two adjacent natural numbers \(p,q \in \mathbb{N}\) such that:
At most \(p\) and at least \(q\) significant digits are lost in the subtraction \(x-y\).
-
Calculate the number of digits lost when calculating \(x-y\) for:
Lost Digits
David Mayerich
STIM Laboratory, University of Houston
You will lose between \(\left\lfloor \frac{-log\left(1 - \frac{y}{x} \right)}{\log b} \right\rfloor\) and \(\left\lceil \frac{-log\left(1 - \frac{y}{x} \right)}{\log b} \right\rceil\)
Loss of Precision Theorem
Calculate the logarithm of all terms
Separate exponents
Multiply by \(-1\) (and reverse inequalities)
Divide by \(\log(b)\)
Lost Digits
David Mayerich
STIM Laboratory, University of Houston
-
Calculate the number of digits lost when calculating \(x-y\) for:
Lost Bits
David Mayerich
STIM Laboratory, University of Houston
-
Calculate the number of bits lost when calculating \(x-y\) for:
Discussion: Precision After Subtraction
-
How many bits of precision are lost when calculating \(\alpha - \sin\alpha\) if \(\alpha = \frac{1}{64}\)?
David Mayerich
STIM Laboratory, University of Houston
-
What precision can we expect if this calculation is performed on
float32Â registers?
| standard | C/C++ | m bits | x bits | bias |
|---|---|---|---|---|
| binary16 | single |
10 | 5 | 15 |
| binary32 | float |
23 | 8 | 127 |
| binary64 | double |
53 | 11 | 1023 |
| binary128 | N/A | 113 | 15 | 16383 |
| binary256 | N/A | 19 | 237 | 262143 |
IEEE 754 Standard
Recovering Precision
Reformulation
Verifying Reformulations
The Quadratic Equation
David Mayerich
STIM Laboratory, University of Houston
Summary of Arithmetic Operations
-
Given \(d\) digits of precision:
-
multiplication:Â preserves \(d\) digits because the exponent and mantissa can be calculated independently
-
addition:Â exponents must be equal, so significant digits may be chopped
-
- Evaluate \(x=1 - \sin 1.6\) using three digits of precision:
David Mayerich
STIM Laboratory, University of Houston
expected result (with 3 digits)
Reformulation
-
Precision loss appears after subtraction (but the loss of data occurs earlier)
-
Reformulate expressions to remove subtractions
David Mayerich
STIM Laboratory, University of Houston
Reformulation (Part Deux)
-
When can catastrophic cancellation occur in the expression \(\sqrt{x+1}-\sqrt{x}\)?
-
When \(x\) is large -- fix it for \(x=10\):
David Mayerich
STIM Laboratory, University of Houston
Original Expression:
Reformulation:
Expected Result:
Quadratic Equation
Loss of Precision
Reformulation
Implementation
David Mayerich
STIM Laboratory, University of Houston
Quadratic Equation
-
Find the roots of a polynomial \(ax^2+bx+c=0\)
-
Quadratic Equation:
David Mayerich
STIM Laboratory, University of Houston
-
Two cases cause computational problems:
-
If \(a=0\), then the equation is undefined (the polynomial isn't quadratic)
-
If \(4ac \ll b^2\) then catastrophic cancellation can occur in the numerator
-
Two cases for cancellation:
-
if \(b>0\) then \(-b+\sqrt{b^2-4ac}\) can result in cancellation
-
if \(b<0\), then \(b-\sqrt{b^2-4ac}\) can result in cancellation
Testing the Quadratic Equation
-
Consider the roots of the polynomial \(x^2 + 182^3x-1\) (using actual floating point):
David Mayerich
STIM Laboratory, University of Houston
-
Performing this calculation using IEEE 754 floating point, we get:
binary64
binary32
Reformulating the Quadratic Equation
-
When \(b \gg 4ac\) there is one very small root
-
Assume that \(b>0\) and reformulate the "small" root:
David Mayerich
STIM Laboratory, University of Houston
Simplifying the Reformulation
-
For the case where \(b > 0\) we have two roots:
David Mayerich
STIM Laboratory, University of Houston
(large root)
(small root)
Implementing the Quadratic Equation
-
Consider the reformulation of both cases for \(b\):
David Mayerich
STIM Laboratory, University of Houston
Series Approximations
Preserving Precision
Mean Value Theorem
David Mayerich
STIM Laboratory, University of Houston
Approximating Special Functions
-
Identify and fix the catastrophic cancellation in the following expression at \(x=0.123\):
David Mayerich
STIM Laboratory, University of Houston
Calculate expansions at \(c=0\):
Reformulate:
Testing the Reformulation
-
Evaluate \(y=\text{sinc}(x) - \cos(x)\) at \(x=0.123\):
David Mayerich
STIM Laboratory, University of Houston
Original Expression:
Reformulation:
Expected Result:
Improve Precision Loss (Part II)
-
Mitigate precision loss in the expression \(y=e^x - e^{-2x}\)
David Mayerich
STIM Laboratory, University of Houston
Mean Value Theorem
If \(f\) is a continuous function on the closed interval \([a,b]\) and possesses a derivative at each point of the open interval \((a,b)\), then
David Mayerich
STIM Laboratory, University of Houston
for some \(\theta\) in \((a,b)\). A basic approximation is \(\theta=\frac{a+b}{2}\).
-
Mitigate catastrophic cancellation in the expression \(y=e^x - e^{x-\epsilon}\):
Test precision for \(x=2\) and \(\epsilon=0.01\):
Original Expression:
Reformulation:
Expected Results:
Mean Value Theorem Examples
-
Reformulate \(y=\ln(x+\epsilon)-\ln x\)
David Mayerich
STIM Laboratory, University of Houston
-
Reformulate \(y=e^\epsilon - 1\)
C.2 Catastrophic Cancellation
By STIM Laboratory
C.2 Catastrophic Cancellation
- 188