【发布时间】:2019-05-13 16:08:53
【问题描述】:
我需要使用 Machin 的方法将 pi 估计到小数点后 100 位,如下所示。 4(4(arctan(1/5)) - arctan(1/239)) = 圆周率。
众所周知,这个公式可以很快收敛到 pi,来源引用了 50 次迭代/项中小数点后 72 位的准确度。
使用 Machin 公式,我只能达到小数点后 15 位的精度,我不知道为什么。
我已经为 arctan(x) 的 Taylor 级数编写了一个函数,然后我在另一个函数中使用了该函数,该函数应用了我上面编写的公式。我还尝试使用小数模块设置更高的精度。
##This is the function for the taylor series of arctan(x)
def arctan(first_term, terms):
k = 0
array = []
if k < 1:
x = (((-1)**k)*(first_term)**((2*k)+1))/((2*k)+1)
k = 1
array.append(x)
if k > 0:
while k < terms:
x = x + (((-1)**k)*(first_term)**((2*k)+1))/((2*k)+1)
k += 1
array.append(x)
return array[-1]
##Here is the function for Machin's formula
def machinpi(first_term, first_term2, terms):
x = 4*(arctan(first_term, terms))-(arctan(first_term2, terms))
return x*4
Machin 以手动将 pi 估计到小数点后 100 位而闻名。我试图弄清楚该系列需要多少个术语才能达到这种准确性。但是,如果我不能先收敛到 pi 的小数点后 100 位,我就找不到答案。使用 Machin 的公式,我希望在 50 次迭代中收敛到小数点后 72 个值。
【问题讨论】:
-
您使用的数字类型似乎不能代表 100 位数字。
-
这就是我所怀疑的。我对python比较陌生。当我调用该函数时,我使用 Decimal 来包装这些值。例如 machinpi(Decimal(1/5), Decimal(1/239), terms),我将小数的精度设置为 100。但似乎这不正确。我应该使用哪种号码类型?
标签: python-3.x math