Anonymous user
Arithmetic-geometric mean: Difference between revisions
m
→{{header|Haskell}}
(OCaml) |
|||
Line 368:
-- The result is considered accurate when two successive approximations are
-- sufficiently close, as determined by "eq".
agm :: (
agm a g eq = snd . head . dropWhile (not . eq) $ iterate step (a, g)
where step (a, g) = ((a + g) / 2, sqrt (a * g))
Line 374:
-- Return the relative difference of the pair. We assume that at least one of
-- the values is far enough from 0 to not cause problems.
relDiff :: (
relDiff (x, y) = let n = abs (x - y)
d = ((abs x) + (abs y)) / 2
|