RosettaCodeData/Task/Arithmetic-geometric-mean/R/arithmetic-geometric-mean-1.r
2023-07-01 13:44:08 -04:00

14 lines
424 B
R

arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))