Section 18.8 Roundoff Error
Roundoff error is the difference between an approximation of a number used in computation and its exact (correct) value. This comes from the computer’s inability to represent some numbers exactly. What may come as a bit of a surprise at first is the fact that there are some rather simple numbers that the computer can’t get right when using the standard ways to store them and not just the obvious candidates such as \(\pi\) or \(e\text{.}\) We’ll see examples below and also why this is happening.
Integers can be represented exactly, but only if they fall into a certain range, depending on the amount of memory used by the data type. Certain floating-point numbers may also be represented exactly, depending on the way floats are represented in the computer in question. Many floating-point numbers cannot be represented exactly.
For example, the decimal number \(0.1\text{,}\) when expressed in binary (only powers of 2 allowed!), is \(0.0011001100110011...\) (it is periodic with \(0011\) repeating forever). Since we can only store a mantissa of a finite width, roundoff will occur no matter how much space is allotted to the data type used.
On the other hand, \(0.125 = \frac{1}{8} = 2^{-3}\text{,}\) and so the binary representation is \(0.001\text{.}\) This number will be accurately represented. Give it a try!