Suppose we had some way to compute \(\pi\) that effectively did the calculation \((e\cdot10^{9}+\pi)-e\cdot10^{9}\text{.}\) Rounding everything to 16 digits, we are computing
\begin{equation*}
(2718281828.459045 +3.141592653589793) -2718281828.459045\text{.}
\end{equation*}
The addition is performed first and the result rounded to 16 digits, giving
\begin{equation*}
2718281831.600637 -2718281828.459045 \text{.}
\end{equation*}
Although roundoff caused some error, it is about a factor of \(10^{-16}\) smaller than the numbers shown. In other words, all but perhaps the last digit shown is correct. Next the subtraction is performed, giving
\begin{equation*}
00 00 00 00 03.141592 \text{.}
\end{equation*}
The leading zeros are not significant, so we lost 9 significant digits and have only 7 left. This type of error, where common leading significant digits cancel, is loss-of-precision (also called loss-of-significance) error. Computers will not display these leading zeros. Instead, the subtraction above yielded
\begin{equation*}
3.141592025756836\text{.}
\end{equation*}
Although it is displayed with 16 digits, only the first 7 are correct.Usually, if you add two numbers of magnitude \(10^{p}\) each with roundoff error \(10^{p-16}\text{,}\) then the result is also of magnitude \(10^{p}\) and the relative error due to roundoff is \(10^{p-16}/10^{p}=10^{-16}\text{.}\) In this example, cancellation made the result of magnitude \(10^{p-q}\text{,}\) so the relative error due to roundoff is \(10^{p-16}/10^{p-q}=10^{q-16}\text{,}\) and so we lost \(q=9\) digits of precision.
This type of loss of precisioncan happen by accident, with catastrophic results, if you are not careful. For example in
\(f'(x)\approx (f(x+h)-f(x))/h\) you will lose precision when
\(h\) gets too small. Try
format long
format compact
f = @(x) x^2
for i = 1:30
h = 10^(-i)
df = (f(1+h)-f(1))/h
relerr = (2-df)/2
end
At first the relative error decreases since truncation error is reduced. Then loss of precision takes over and the relative error increases to 1. This happens because when
\(f(1)\) and
\(f(1+h)\) become close, the subtraction ``cancels’’ digits.