RosettaCodeData/Task/Arithmetic-geometric-mean/Python/arithmetic-geometric-mean-2.py

11 lines
268 B
Python

from decimal import Decimal, getcontext
def agm(a, g, tolerance=Decimal("1e-65")):
while True:
a, g = (a + g) / 2, (a * g).sqrt()
if abs(a - g) < tolerance:
return a
getcontext().prec = 70
print agm(Decimal(1), 1 / Decimal(2).sqrt())