% Salamin and Brent formula for pi.
% Based on the arithmetic-geometric mean.
% Convergence is quadratic.
% iteration will give full 16-digit accuracy.
% K. Ming Leung, 02/24/04

clear; format long;
a = 1;
b = 1/sqrt(2);
s = 1/2; twok = 1;
disp('       pi                error');
for k = 1:5
    twok = 2*twok;
    aTemp = 0.5*(a+b);
    b = sqrt(a*b);
    a = aTemp;
    s = s - twok*(a*a-b*b);
    p=2*a*a/s;
    disp([p p-pi]);
end

