Guard digit

From HandWiki
Revision as of 19:33, 6 February 2024 by WikiGary (talk | contribs) (add)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In numerical analysis, one or more guard digits can be used to reduce the amount of roundoff error.

For example, suppose that the final result of a long, multi-step calculation can be safely rounded off to N decimal places. That is to say, the roundoff error introduced by this final roundoff makes a negligible contribution to the overall uncertainty.

However, it is quite likely that it is not safe to round off the intermediate steps in the calculation to the same number of digits. Be aware that roundoff errors can accumulate. If M decimal places are used in the intermediate calculation, we say there are M−N guard digits.

Guard digits are also used in floating point operations in most computer systems. Given [math]\displaystyle{ 2^1 \times 0.100_2 - 2^0 \times 0.111_2 }[/math] we have to line up the binary points. This means we must add an extra digit to the first operand—a guard digit. This gives us [math]\displaystyle{ 2^1 \times 0.1000_2 - 2^1 \times 0.0111_2 }[/math]. Performing this operation gives us [math]\displaystyle{ 2^1 \times 0.0001_2 }[/math] or [math]\displaystyle{ 2^{-2} \times 0.100_2 }[/math]. Without using a guard digit we have [math]\displaystyle{ 2^1 \times 0.100_2 - 2^1 \times 0.011_2 }[/math], yielding [math]\displaystyle{ 2^1 \times 0.001_2= }[/math] or [math]\displaystyle{ 2^{-1} \times 0.100_2 }[/math]. This gives us a relative error of 1. Therefore, we can see how important guard digits can be.

An example of the error caused by floating point roundoff is illustrated in the following C code.

int main(){
   double a;
   int i;

   a = 0.2; 
   a += 0.1; 
   a -= 0.3;

   for (i = 0; a < 1.0; i++) 
       a += a;

   printf("i=%d, a=%f\n", i, a);

   return 0;
}

It appears that the program should not terminate. Yet the output is:

i=54, a=1.000000

Another example is:

Take two numbers:

[math]\displaystyle{ 2.56\times 10^0 }[/math] and [math]\displaystyle{ 2.34\times 10^2 }[/math]

We bring the first number to the same power of [math]\displaystyle{ 10 }[/math] as the second one:

[math]\displaystyle{ 0.0256\times 10^2 }[/math]

The addition of the two numbers is:

0.0256*10^2 
2.3400*10^2 +  
____________ 
2.3656*10^2 

After padding the second number (i.e., [math]\displaystyle{ 2.34\times 10^2 }[/math]) with two [math]\displaystyle{ 0 }[/math]s, the bit after [math]\displaystyle{ 4 }[/math] is the guard digit, and the bit after is the round digit. The result after rounding is [math]\displaystyle{ 2.37 }[/math] as opposed to [math]\displaystyle{ 2.36 }[/math], without the extra bits (guard and round bits), i.e., by considering only [math]\displaystyle{ 0.02+2.34 = 2.36 }[/math]. The error therefore is [math]\displaystyle{ 0.01 }[/math].

See also

References

  • Forman S. Acton. Numerical Methods that Work, The Mathematical Association of America (August 1997).
  • Higham, Nicholas J. Accuracy and Stability of Numerical Algorithms, Washington D.C.: Society for Industrial & Applied Mathematics, 2002.
  • IEEE 754 round-off errors