require 'flt'
include Flt

BinNum.Context.precision = 512  

def agm(a,g)
  new_a = BinNum a
  new_g = BinNum g
  while new_a - new_g > new_a.class.Context.epsilon do
    old_g = new_g
    new_g = (new_a * new_g).sqrt
    new_a = (old_g + new_a) * 0.5
  end
  new_g
end

puts agm(1, 1 / BinNum(2).sqrt)