Arithmetic-geometric mean: Difference between revisions

m
(OCaml)
Line 368:
-- The result is considered accurate when two successive approximations are
-- sufficiently close, as determined by "eq".
agm :: (Ord a, Floating a) => a -> a -> ((a, a) -> Bool) -> a
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 :: (Ord a, Fractional a) => (a, a) -> a
relDiff (x, y) = let n = abs (x - y)
d = ((abs x) + (abs y)) / 2
Anonymous user