Python 3, 256 bytes
This version computes pi using an accelerated version of Archimedes' algorithm for pi = 4*arctan(1)
.
m=338
M=1<<m
def s(y,x=M):
y<<=m;u=x
while u<0 or u>1:u=(x//2+y)//x-x;x+=u>>1
return x
a=b=x=M
g=s(2*M)
d=[a]
for i in range(16):
a=(a+g)>>1;p,d=d,[a];q=0
for u in p:q+=2;d+=[d[-1]-(u>>q)]
b-=b>>q;g=s(a*g>>m,g)
print(str(b*x//d[-1]*4*10**100>>m)[1:])
Try it in SageMathCell
This code implements binary fixed point arithmetic using Python integers. The core arctan algorithm was devised by Bille Carlson, see Carlson, B. C. An Algorithm for Computing Logarithms and Arctangents, Mathematics of Computation, Apr 1972.
This algorithm rapidly calculates any of the standard inverse circular and hyperbolic functions. It even handles complex arguments (assuming the sqrt function does). Here's a non-golfed demo that computes pi = 10*arcsin((sqrt(5)-1)/4)
.
This algorithm is very similar to the AGM (arithmetic-geometric mean), but it updates the two core terms a
and g
sequentially instead of in parallel. So it's sometimes referred to as the fake AGM. It's also known as Borchardt's modified AGM algorithm. The Borwein brothers pointed out that it is, in fact, the same algorithm Archimedes used to iteratively subdivide a polygon.
Carlson's innovation is to apply Richardson series acceleration, which speeds up the convergence considerably. It's not as fast as a true AGM, however it's quite efficient for high precision calculation because it only uses one non-trivial division. (The square root can be also be implemented to avoid non-trivial divisions, using the Quake-style 1/sqrt algorithm).
For further details on algorithms related to pi and the AGM, please see The Borwein brothers, Pi and the AGM by Richard P. Brent.
FWIW, Carlson also developed and analysed a suite of true AGM algorithms for computing elliptic integrals and functions, and those algorithms can (of course) handle circular & hyperbolic functions. See Numerical computation of real or complex elliptic integrals