【问题标题】:Accuracy problems in estimating pi using Machin's method使用 Machin 方法估计 pi ​​的准确性问题
【发布时间】: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


【解决方案1】:

好的,我已经解决了这个问题。我不知道为什么在 python 中会发生这种情况,但是调用我写的这个函数 machinpi(Decimal(1/5), Decimal(1/239), terms) 不等于这个具有我期望 machinpi(小数(1)/小数(5),小数(1)/小数(239),项)

【讨论】:

  • 在 Python 中,函数的参数总是evaluated before the function is called。所以Decimal(1/5) 评估1/5 作为浮点数 然后将该浮点数(不完全是0.2)传递给Decimal。
  • 哦,我明白了。感谢您提供这些信息,它肯定会在未来派上用场。
猜你喜欢
  • 2019-09-06
  • 2023-04-03
  • 2016-04-22
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多