What a Computer Does to a Number
The Subtraction That Loses Every Correct Digit at Once
Last timeError That Grows With the Work
A difference between near-equal values magnifies the error already inside them by the ratio of their size to the difference, and two standard formulas are rewritten so it cannot happen.
Two values agree to nine decimal digits. Subtract them, and the nine digits
they agreed on are gone from the answer and the digits they disagreed on are
all that is left. That much is obvious and is not the problem. The problem is
that the digits they disagreed on were never right in the first place, because
each value was only ever stored to within half a gap, and the subtraction has
just promoted that half gap from a rounding nobody could see into the entire
result.
This is the one operation in the whole of machine arithmetic that turns a small
relative error into a large one. Every other operation is tame. Multiply two
values each carrying a relative error of about and the product
carries about . Divide, take a square root, take a cosine,
and the same holds. Subtract two values that are nearly equal and the relative
error can be anything at all, including one hundred per cent.
The subtraction is not where the error comes from
Write for the unit roundoff of the 64 bit form, which is , or
0.00000000000000011102230246251565. That is the largest relative error a single
correctly rounded operation can leave behind, because the gap between
neighbouring values at magnitude is at most and rounding
to nearest lands within half of it.
Let and be the exact values we meant, and and the
values the machine actually holds, so that and
with and .
Here is the step that almost every explanation of this subject skips, and it is
the step that makes the rest make sense. If and lie within
a factor of two of each other, their difference is computed exactly. No
rounding happens. This is Sterbenz's lemma, and the reason is that the
difference of two such values needs no more significant bits than the smaller
of them already has, so the pattern for it already exists and the subtraction
simply writes it down.
So the computed difference is not an approximation of . It
is , which is
$$
The absolute error in the difference is , and it is
exactly the absolute error the operands brought with them. Nothing was added
and nothing was taken away. What changed is what that absolute error is being
compared against, because is tiny and and are not.
Following it through one minus the cosine
Take the quantity at . As a limit it tends to
one half, so the honest answer is a trifle under 0.5, and the machine has every
digit it needs to deliver that.
The cosine is computed first. Its true value at this is
0.999999995000000004166666186, and the stored result is 0.999999995, whose
exact value as a real number is 0.999999995000000030387354855. The absolute
error is therefore 0.0000000000000000262206886685, or
. That is 0.236 of the half gap the standard allows at
this magnitude, so the cosine routine did its job and better than it had to.
Now subtract. The ratio is 1.000000005, comfortably inside
the factor of two, so the subtraction is exact. The true value of
is and what comes back is
. Their difference is
, the same absolute error as before, to every digit.
But as a fraction of the result it is no longer . It is
, which is
. A relative error of five parts in a hundred million,
produced by an operation that rounded nothing.
The lesson stops here
6 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents