
Calculating pi with AGM and mpmath - ColinWright
http://www.johndcook.com/blog/2012/06/03/calculating-pi-with-agm-and-mpmath/
======
sltkr
The idea is nice, but the code is a little flaky. For example, change the
number of decimals from 1000 to 2000 and it enters an infinite loop, but 4000
works again. This occurs because due to rounding a and b might not converge to
the exact same value.

A more reliable way to write the algorithm is this:

    
    
        n = 0
        while not almosteq(a, b):
            n, a, b = n + 1, (a + b)/2, sqrt(a*b)
            series += 2**(n+1) * (a*a - b*b)

~~~
johndcook
Good catch. I updated the post so that it tests for a > b + epsilon instead of
a == b.

