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

11 lines
268 B
Python
Raw Permalink Normal View History

2013-04-10 14:58:50 -07:00
from decimal import Decimal, getcontext
def agm(a, g, tolerance=Decimal("1e-65")):
while True:
2014-04-02 16:56:35 +00:00
a, g = (a + g) / 2, (a * g).sqrt()
2013-04-10 14:58:50 -07:00
if abs(a - g) < tolerance:
return a
getcontext().prec = 70
print agm(Decimal(1), 1 / Decimal(2).sqrt())