Code from class

code
Week02
Published

January 22, 2026

# Assume that you are using 𝛽=10 and 𝑝=3


# What is the relative error of the expression x^2 - y^2
# for 𝑥=3.34,𝑦=3.33
f <- function(x, y, p) {
  x <- signif(x, p)
  y <- signif(y, p)

  signif(signif(x^2,p) - signif(y^2, p), p)
}

(f(3.34, 3.33, p = 3) - f(3.34, 3.33, p = 17))/f(3.34, 3.33, p = 17)

f(3.34, 3.33, p = 17)

#  How does the relative error change for (𝑥−𝑦)(𝑥+𝑦)
# for 𝑥=3.34,𝑦=3.33

g <- function(x, y, p) {
  x <- signif(x, p)
  y <- signif(y, p)

  signif(signif(x +y,p) * signif(x-y, p), p)
}

g(3.34, 3.33, p = 3)

g(3.34, 3.33, p = 17)