agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
  where
    step (a, g) = ((a + g) / 2, sqrt (a * g))



relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
  let n = abs (x - y)
      d = (abs x + abs y) / 2
  in n / d

main :: IO ()
main = do
  let equal = (< 0.000000001) . relDiff
  print $ agm 1 (1 / sqrt 2) equal