x <- seq(-3/4, 3/4, by=1/1024)
plot(x, rexpm1(x)/expm1(x) - 1, type="l", main = "Error wrt expm1()")
abline(h = (-8:8)*2^-53, lty=1:2, col=adjustcolor("gray", 1/2))
cb2 <- adjustcolor("blue", 1/2)
do.15 <- function(col = cb2) {
abline(v = 0.15*(-1:1), lty=3, lwd=c(3,1,3), col=col)
axis(1, at=c(-.15, .15), col=cb2, col.axis=cb2)
}
do.15()
op <- par(mar = par("mar") + c(0,0,0,2))
plot(x, abs(rexpm1(x)/expm1(x) - 1),type="l", log = 'y',
main = "*Relative* Error wrt expm1() [log scale]")#, yaxt="n"
abline(h = (1:9)*2^-53, lty=2, col=adjustcolor("gray", 1/2))
axis(4, at = (1:9)*2^-53, las = 1, labels =
expression(2^-53, 2^-52, 3 %*% 2^-53, 2^-51, 5 %*% 2^-53,
6 %*% 2^-53, 7 %*% 2^-53, 2^-50, 9 %*% 2^-53))
do.15()
par(op)
## "True" Accuracy comparison of rexpm1() with [OS mathlib based] expm1():
if(require("Rmpfr")) withAutoprint({
xM <- mpfr(x, 128); Xexpm1 <- expm1(xM)
REr1 <- asNumeric(rexpm1(x)/Xexpm1 - 1)
REe1 <- asNumeric(expm1(x) /Xexpm1 - 1)
absC <- function(E) pmax(2^-55, abs(E))
plot(x, absC(REr1), type= "l", log="y",
main = "|rel.Error| of exp(x)-1 computations wrt 128-bit MPFR ")
lines(x, absC(REe1), col = (c2 <- adjustcolor(2, 3/4)))
abline(h = (1:9)*2^-53, lty=2, col=adjustcolor("gray60", 1/2))
do.15()
axis(4, mgp=c(2,1/4,0),tcl=-1/8, at=2^-(53:51), labels=expression(2^-53, 2^-52, 2^-51), las=1)
legend("topleft", c("rexpm1(x)", " expm1(x)"), lwd=2, col=c("black", c2),
bg = "gray90", box.lwd=.1)
})
Run the code above in your browser using DataLab