Ch2.3.1 Round-Off Error & Machine Epsilon

Ch2.3 Numerical Error Intro

  • When using a calculator in a math or science class, we rely on the accuracy of the results.
  • Step 1 for the calculator is to receive and store the numbers being entered.
  • Step 2 is to perform the operation(s) requested.
  • In this section we continue our investigation into how numbers are stored (Step 1) and what kind of errors arise as a result of this basic step.

Example

  • For the calculation below, what are the numbers being entered, and what are the operations required?
log(pi)
[1] 1.14473
  • What are the challenges here for the computer (and numerical analyst)?

Ch2.3 Numerical Error Intro

  • In addition to accuracy and precision in our source numbers, idiosyncrasies of floating point formats can create new errors.
  • As we have seen, some numbers can be represented precisely while others cannot.
  • This mixing and matching of numbers and storage types can lead to new errors, above and beyond measurement error, that we must be able to plan and account for.
c(1/4,pi,1/0,sqrt(-2),NA)
[1] 0.250000 3.141593      Inf      NaN       NA

Machine Epsilon

  • Another characteristic of number storage is machine error.
  • Most numbers \( x \) on number line have no machine representation.
  • Machine error takes into account spacing between floating point numbers.

title

Machine Epsilon

  • The spacing of binary numbers on the number line varies:

title.

  • For any \( x \), next larger number is not \( x \) + computer min, because every \( x \) is represented by floating point format:

\[ x = (-1)^s 2^{c-1023} (1 + f) \]

Biggest Floating Point

  • As we know, floating point cannot represent every number between 0 and 1.79769313486232e+308.
mantissa <- rep(1,52)
powers <- seq(from = 1, to = 52, by = 1)
f <- sum(mantissa*(1/2)^(powers))
2^(1023)*(1 + f)
[1] 1.797693e+308

title

Smallest Positive Floating Point

  • Calculate smallest positive floating point number:
c <- 0
f <- 2^(-52)
2^(c - 1023)*(1 + f)
[1] 1.112537e-308

title

Machine Epsilon

  • The machine error, frequently just called machine \( \epsilon \), captures the spacing of numbers within the floating point space.
  • Machine \( \epsilon \) at \( x \) actually depends on \( x \), and is written \( \epsilon_x \)

title

Machine Epsilon

  • Machine \( \epsilon \) is understood to be for \( x = 1 \), with \( \epsilon = \epsilon_1 \).

title

title

Machine Epsilon

  • Thus machine \( \epsilon \) is not the smallest positive number that can be represented in floating point format.
  • Smallest number, from a few slides back:
[1] 1.112537e-308
  • Rather \( \epsilon \) = gap between 1 and next greater number.

title

Machine Epsilon

  • Thus the smallest interval \( [a,b] \) on the binary number line starting at \( a = 1 \) would be
[a, b] = [1, 1 + epsilon]

title

Machine Epsilon Definition

  • Machine \( \epsilon \) is defined as the smallest value such that

\[ 1 + \epsilon > 1 \]

title

Machine Epsilon Discussion

  • Mathematically, \( 1 + x > 1 \) for any \( x > 0 \).
  • But with the number spacing within floating point, that is not necessarily the case on a computer.
  • Thus machine \( \epsilon \) is the threshold value for representation.

title

Machine Epsilon in R

  • In R, we can find machine \( \epsilon \) as follow:
(eps <- .Machine$double.eps)
[1] 2.220446e-16

title

Machine Epsilon in R

  • Machine \( \epsilon \) is defined as the smallest value such that

\[ 1 + \epsilon > 1 \]

1 + eps
[1] 1
  • Definition doesn't appear to be satisfied here in R.

Machine Epsilon

  • Let's see more digits:
eps
[1] 2.220446e-16
print(1 + eps,digits = 20)
[1] 1.0000000000000002

title

Machine Epsilon

  • Let's see if

\[ 1 + 0.5\epsilon \ngtr 1 \]

print(1 + eps/2,digits = 20)
[1] 1

title

Machine Epsilon in R

  • It looks like R is rounding up on this example:
eps
[1] 2.220446e-16
print(1 + 0.6*eps,digits = 20)
[1] 1.0000000000000002

title

Machine Representation & Associativity

  • In mathematics, addition is associative:

\[ a + (b + c) = (a + b) + c \]

  • However, due to limitations on machine number representation, computer addition isn't always associative.

title

Machine Representation & Associativity

  • As an example, see if you can explain these results:
print((1 + eps/2) + eps/2, digits = 20)
[1] 1
print(1 + (eps/2 + eps/2), digits = 20)
[1] 1.0000000000000002

title

Negative Machine Epsilon

  • Negative machine epsilon is the smallest \( \epsilon > 0 \) such that

\[ 1 - \epsilon < 1 \]

  • Here's the command in R:
.Machine$double.neg.eps
[1] 1.110223e-16

General Machine Epsilon

  • For general \( x \), machine epsilon is smallest \( \epsilon_x > 0 \) such that

\[ x + \epsilon_x > x \]

title

Example

  • For \( x = 8 \), we can infer here that \( \epsilon_8 > \epsilon \):
print(8 + eps, digits = 20)
[1] 8

title

Package: pracma

  • The library \( \textsf{pracma} \) contains a function, \( \textsf{eps} \), that provides the value of \( \epsilon_x \) for any given value of \( x \).
library(pracma)
eps(8)
[1] 1.776357e-15

title

Example

(e <- eps(8))
[1] 1.776357e-15
print(8 + e, digits = 20)
[1] 8.0000000000000018

title

General Machine Epsilon

  • The value of \( \epsilon_x \) is directly proportional to the value of \( x \).
c(eps(4), eps(8))
[1] 8.881784e-16 1.776357e-15
2*0.8882
[1] 1.7764

title

Round-Off and Machine Epsilon

  • Most values of \( x \) on the number line continuum cannot be represented precisely on a computer.
  • This is due the tolerance limitations specified by \( \epsilon_x \) and/or the impossibility of storing an infinite number of decimal digits.
c(1/4,22/7,pi,sqrt(2))
[1] 0.250000 3.142857 3.141593 1.414214

title

Round-Off Error Definition

  • Let \( x \) = actual number
  • Let \( x'= \mathrm{fl}(x) \) = floating point representation of \( x \).
  • Then the round-off error \( E_R \) for a number \( x \) is given by

\[ E_R = |x - \mathrm{fl}(x)| \]

title

Examples: Round-Off Error

(x <- c(1/4,22/7,pi,sqrt(2)))
[1] 0.250000 3.142857 3.141593 1.414214
(y <- round(x,2))
[1] 0.25 3.14 3.14 1.41
(E <- abs(x-y))
[1] 0.000000000 0.002857143 0.001592654 0.004213562

Round-Off Error Occurs in Other Ways Too

  • A number may be rounded by a person when writing down a measurement.
  • A number may be rounded by a person when entering data into the computer.
  • A number may be rounded by the computer when receiving and storing the number from a person who enters it (for example, with \( x = 1/3 \)).
  • A number may be rounded in the middle of a lengthy sequence of computations when implementing an algorithm.