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
Losing Digits to Cancellation
Loss of Precision Theorem
David Mayerich
STIM Laboratory, University of Houston
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
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
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:
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)\)
David Mayerich
STIM Laboratory, University of Houston
Calculate the number of digits lost when calculating \(x-y\) for:
David Mayerich
STIM Laboratory, University of Houston
Calculate the number of bits lost when calculating \(x-y\) for:
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
Reformulation
Verifying Reformulations
The Quadratic Equation
David Mayerich
STIM Laboratory, University of Houston
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
David Mayerich
STIM Laboratory, University of Houston
expected result (with 3 digits)
Precision loss appears after subtraction (but the loss of data occurs earlier)
Reformulate expressions to remove subtractions
David Mayerich
STIM Laboratory, University of Houston
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:
Loss of Precision
Reformulation
Implementation
David Mayerich
STIM Laboratory, University of Houston
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
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
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
For the case where \(b > 0\) we have two roots:
David Mayerich
STIM Laboratory, University of Houston
(large root)
(small root)
Consider the reformulation of both cases for \(b\):
David Mayerich
STIM Laboratory, University of Houston
Preserving Precision
Mean Value Theorem
David Mayerich
STIM Laboratory, University of Houston
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:
Evaluate \(y=\text{sinc}(x) - \cos(x)\) at \(x=0.123\):
David Mayerich
STIM Laboratory, University of Houston
Original Expression:
Reformulation:
Expected Result:
Mitigate precision loss in the expression \(y=e^x - e^{-2x}\)
David Mayerich
STIM Laboratory, University of Houston
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:
Reformulate \(y=\ln(x+\epsilon)-\ln x\)
David Mayerich
STIM Laboratory, University of Houston
Reformulate \(y=e^\epsilon - 1\)