def determinePrecision(pi): pi = str(pi) pi_ref = str(gmpy.pi(10000)) # reference pi min_len = min(len(pi), len(pi_ref)) i = 0 while i < min_len and pi[i] == pi_ref[i]: i += 1 return i
#variable precision trigonometry functions import gmpy import numpy from matplotlib import pyplot import pickle import math N=1000 pi=gmpy.pi(N) pi2=pi/2 pi2_minus = -pi2 pix2 = pi*2 pi_minus = -pi mpone=gmpy.mpf(1,N) eps=gmpy.mpf(2)**(-N) def sin_taylor(x): x2 = x*x s=0 i=2 an = x sgn = 1 while True: s = s + sgn*an an = an*x2/(i*(i+1)) i += 2
/____________ n=0 There are three main calculations in this Javascript program: - Taylor series for the square root of 8, - Ramanujan's series for 1/Pi, - Newton-Raphson method for the reciprocal. """ fact_cache = [1,1,2,6,24,120,720,5040] def fact(n): if n < len(fact_cache): return fact_cache[n] return n*fact(n-1) def rama(nsteps,prec=10): from gmpy import mpf pre = mpf('8',prec).sqrt()/mpf('9801',prec) sum = mpf('0',prec) for i in range(nsteps): num = fact(4*i)*(1103+26390*i) den = pow(fact(i),4)*pow(396,4*i) sum += mpf(num,prec)/mpf(den,prec) print mpf(1,prec)/(pre*sum) if __name__ == '__main__': from gmpy import pi print pi(1000) rama(100,1000)
def test_pi(): from gmpy import pi # import the Gnu Multiprecision library pi function val = pi(1000000) # ~300,000 digits of pi digpi = digits_to_list(val)