我的代码输出的pi值不正确...
#!/usr/bin/python
import sys
import gmpy2
from gmpy2 import mpfr
n = int(sys.argv[1])
l = int(sys.argv[2])
gmpy2.get_context().precision = n * 4
p0 = mpfr(1)
q0 = mpfr(1)
t0 = mpfr(0)
p = mpfr(0)
q = mpfr(0)
t = mpfr(0)
a = mpfr(0)
for loop in range(1, l+1, 1):
p = (2*loop - 1)*(6*loop - 1)*(6*loop - 5)
q = (loop**3)*(640320**3)/24
a = (13591409 + 545140134*loop)
if loop % 2 == 0:
a = -1 * a
else:
a = 1 * a
t = t0*q + a*p
p = p*p0
q = q*q0
p0 = p
q0 = q
t0 = t
pi = 640320 * gmpy2.sqrt(640320)*q
pi = pi/(12*(t + 13591409*q))
print(pi)
我认为我的算法是正确的。只是为了确定,如果不正确,请告诉我。
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066486
预期输出:
{{1}}