Arithmetic-geometric mean: Difference between revisions
Content added Content deleted
(OCaml) |
|||
Line 368: | Line 368: | ||
-- The result is considered accurate when two successive approximations are |
-- The result is considered accurate when two successive approximations are |
||
-- sufficiently close, as determined by "eq". |
-- sufficiently close, as determined by "eq". |
||
agm :: ( |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a |
||
agm a g eq = snd . head . dropWhile (not . eq) $ iterate step (a, g) |
agm a g eq = snd . head . dropWhile (not . eq) $ iterate step (a, g) |
||
where step (a, g) = ((a + g) / 2, sqrt (a * g)) |
where step (a, g) = ((a + g) / 2, sqrt (a * g)) |
||
Line 374: | Line 374: | ||
-- Return the relative difference of the pair. We assume that at least one of |
-- 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. |
-- the values is far enough from 0 to not cause problems. |
||
relDiff :: ( |
relDiff :: (Fractional a) => (a, a) -> a |
||
relDiff (x, y) = let n = abs (x - y) |
relDiff (x, y) = let n = abs (x - y) |
||
d = ((abs x) + (abs y)) / 2 |
d = ((abs x) + (abs y)) / 2 |