Basis Lab

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 10−1610^{-16} and the product

carries about 2×10−162 \times 10^{-16}. 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 uu for the unit roundoff of the 64 bit form, which is 2−532^{-53}, 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 mm is at most m⋅2−52m \cdot 2^{-52} and rounding

to nearest lands within half of it.

Let aa and bb be the exact values we meant, and a^\hat{a} and b^\hat{b} the

values the machine actually holds, so that a^=a(1+δa)\hat{a} = a(1 + \delta_a) and

b^=b(1+δb)\hat{b} = b(1 + \delta_b) with ∣δa∣≤u|\delta_a| \le u and ∣δb∣≤u|\delta_b| \le u.

Here is the step that almost every explanation of this subject skips, and it is

the step that makes the rest make sense. If a^\hat{a} and b^\hat{b} 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 a^−b^\hat{a} - \hat{b}. It

is a^−b^\hat{a} - \hat{b}, which is

$a^−b^=(a−b)+(aδa−bδb).\hat{a} - \hat{b} = (a - b) + (a \delta_a - b \delta_b).$

The absolute error in the difference is ∣aδa−bδb∣|a \delta_a - b \delta_b|, 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 a−ba - b is tiny and aa and bb are not.

Following it through one minus the cosine

Take the quantity (1−cos⁡x)/x2(1 - \cos x) / x^2 at x=0.0001x = 0.0001. 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 xx 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

2.622×10−172.622 \times 10^{-17}. 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 1/0.9999999951 / 0.999999995 is 1.000000005, comfortably inside

the factor of two, so the subtraction is exact. The true value of 1−cos⁡x1 - \cos x

is 4.999999995833334×10−94.999999995833334 \times 10^{-9} and what comes back is

4.999999969612645×10−94.999999969612645 \times 10^{-9}. Their difference is

2.622×10−172.622 \times 10^{-17}, the same absolute error as before, to every digit.

But as a fraction of the result it is no longer 2.622×10−172.622 \times 10^{-17}. It is

2.622×10−17/4.9999999958×10−92.622 \times 10^{-17} / 4.9999999958 \times 10^{-9}, which is

5.244×10−95.244 \times 10^{-9}. 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 contents

This 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

The rest of this course

  1. 01Why Eight Bits Hold 256 Values and Minus 128 Has No Positive Twin
  2. 02What Really Happens When a Sum Does Not Fitopening only
  3. 03Reading a Real Number Out of Its Bits, and Writing One Tenth Back Inopening only
  4. 04The Gap to the Next Representable Number, and How to Compute Itopening only
  5. 05One Rounding Is Half a Gap, and a Tenth Plus Two Tenths Is Exactly a Tieopening only
  6. 06Error in a Long Sum Grows Like Root n, Not Like nopening only
  7. 07The Subtraction That Loses Every Correct Digit at Onceyou are here
  8. 08Two Zeros, Two Infinities, and a Value Not Equal to Itselfopening only

Read alongside