如何确定我的pi计算是否准确?

本文翻译自:How do I determine whether my calculation of pi is accurate?

I was trying various methods to implement a program that gives the digits of pi sequentially. 我正在尝试各种方法来实现一个顺序给出pi数字的程序。 I tried the Taylor series method, but it proved to converge extremely slowly (when I compared my result with the online values after some time). 我尝试了泰勒系列方法,但事实证明它收敛得非常慢(当我在一段时间后将我的结果与在线值进行比较时)。 Anyway, I am trying better algorithms. 无论如何,我正在尝试更好的算法。

So, while writing the program I got stuck on a problem, as with all algorithms: How do I know that the n digits that I've calculated are accurate? 因此,在编写程序时,我遇到了问题,就像所有算法一样:我怎么知道我计算的n位数是准确的?


#1楼

参考:https://stackoom.com/question/xvjK/如何确定我的pi计算是否准确


#2楼

You could use multiple approaches and see if they converge to the same answer. 您可以使用多种方法,看看它们是否收敛到相同的答案。 Or grab some from the 'net. 或者从网上抓一些。 The Chudnovsky algorithm is usually used as a very fast method of calculating pi. Chudnovsky算法通常用作计算pi的非常快速的方法。 http://www.craig-wood.com/nick/articles/pi-chudnovsky/ http://www.craig-wood.com/nick/articles/pi-chudnovsky/


#3楼

The Taylor series is one way to approximate pi. 泰勒系列是一种近似pi的方法。 As noted it converges slowly. 如上所述,它收敛缓慢。

The partial sums of the Taylor series can be shown to be within some multiplier of the next term away from the true value of pi. 泰勒级数的部分和可以显示在下一个项的某个乘数内,远离pi的真值。

Other means of approximating pi have similar ways to calculate the max error. 其他近似pi的方法有类似的方法来计算最大误差。

We know this because we can prove it mathematically. 我们知道这一点,因为我们可以用数学证明它。


#4楼

Since I'm the current world record holder for the most digits of pi, I'll add my two cents : 由于我是目前pi数字最多的世界纪录保持者,我将加上我的两分钱

Unless you're actually setting a new world record, the common practice is just to verify the computed digits against the known values. 除非您实际设置新的世界记录,否则通常的做法是仅根据已知值验证计算的数字。 So that's simple enough. 所以这很简单。

In fact, I have a webpage that lists snippets of digits for the purpose of verifying computations against them: http://www.numberworld.org/digits/Pi/ 实际上,我有一个网页列出了数字片段,目的是验证对它们的计算: http//www.numberworld.org/digits/Pi/


But when you get into world-record territory, there's nothing to compare against. 但是当你进入世界纪录领域时,没有什么可比的。

Historically, the standard approach for verifying that computed digits are correct is to recompute the digits using a second algorithm. 从历史上看,验证计算数字是否正确的标准方法是使用第二种算法重新计算数字。 So if either computation goes bad, the digits at the end won't match. 因此,如果任一计算变坏,则末尾的数字将不匹配。

This does typically more than double the amount of time needed (since the second algorithm is usually slower). 这通常会使所需时间增加一倍以上(因为第二种算法通常较慢)。 But it's the only way to verify the computed digits once you've wandered into the uncharted territory of never-before-computed digits and a new world record. 但是,一旦你徘徊在未经计算的数字和新的世界纪录的未知领域,这是验证计算数字的唯一方法。


Back in the days where supercomputers were setting the records, two different AGM algorithms were commonly used: 在超级计算机设置记录的日子里,常用两种不同的AGM算法

These are both O(N log(N)^2) algorithms that were fairly easy to implement. 这些都是O(N log(N)^2)算法,相当容易实现。

However, nowadays, things are a bit different. 然而,如今,事情有点不同。 In the last three world records, instead of performing two computations, we performed only one computation using the fastest known formula ( Chudnovsky Formula ): 在最后三个世界纪录中,我们使用最快的已知公式( Chudnovsky公式 )执行了一次计算,而不是执行两次计算:

在此输入图像描述

This algorithm is much harder to implement, but it is a lot faster than the AGM algorithms. 该算法难以实现,但它比AGM算法快得多。

Then we verify the binary digits using the BBP formulas for digit extraction . 然后我们使用BBP公式验证二进制数字以进行数字提取

在此输入图像描述

This formula allows you to compute arbitrary binary digits without computing all the digits before it. 此公式允许您计算任意二进制数字而不计算它之前的所有数字。 So it is used to verify the last few computed binary digits. 因此它用于验证最后几个计算的二进制数字。 Therefore it is much faster than a full computation. 因此,这是不是一个完整的运算速度要快得多

The advantage of this is: 这样做的好处是:

  1. Only one expensive computation is needed. 只需要一个昂贵的计算。

The disadvantage is: 缺点是:

  1. An implementation of the Bailey–Borwein–Plouffe (BBP) formula is needed. 需要实施Bailey-Borwein-Plouffe (BBP)公式。
  2. An additional step is needed to verify the radix conversion from binary to decimal. 需要额外的步骤来验证从二进制到十进制的基数转换。

I've glossed over some details of why verifying the last few digits implies that all the digits are correct. 我已经掩盖了为什么验证最后几位数意味着所有数字都正确的一些细节。 But it is easy to see this since any computation error will propagate to the last digits. 但很容易看到这一点,因为任何计算错误都会传播到最后的数字。


Now this last step (verifying the conversion) is actually fairly important. 现在最后一步(验证转换)实际上非常重要。 One of the previous world record holders actually called us out on this because, initially, I didn't give a sufficient description of how it worked. 之前的世界纪录保持者之一实际上已经打电话给我们,因为最初我没有充分描述它是如何运作的。

So I've pulled this snippet from my blog: 所以我从我的博客中提取了这个片段:

N = # of decimal digits desired
p = 64-bit prime number

在此输入图像描述

Compute A using base 10 arithmetic and B using binary arithmetic. 使用基数10算术计算A,使用二进制算术计算B.

在此输入图像描述

If A = B , then with "extremely high probability", the conversion is correct. 如果A = B ,则“极高概率”,转换是正确的。


For further reading, see my blog post Pi - 5 Trillion Digits . 如需进一步阅读,请参阅我的博客文章Pi - 5 Trillion Digits


#5楼

Undoubtedly, for your purposes (which I assume is just a programming exercise), the best thing is to check your results against any of the listings of the digits of pi on the web. 毫无疑问,出于您的目的(我假设只是一项编程练习),最好的办法是检查您的结果是否与网络上pi的任何数字列表相对应。

And how do we know that those values are correct? 我们怎么知道那些价值是正确的? Well, I could say that there are computer-science-y ways to prove that an implementation of an algorithm is correct. 好吧,我可以说有计算机科学的方法可以证明算法的实现是正确的。

More pragmatically, if different people use different algorithms, and they all agree to (pick a number) a thousand (million, whatever) decimal places, that should give you a warm fuzzy feeling that they got it right. 更务实的是,如果不同的人使用不同的算法,并且他们都同意(选择一个数字)千位(百万,无论如何)小数位,这应该给你一种温暖的模糊感觉,他们做对了。

Historically, William Shanks published pi to 707 decimal places in 1873. Poor guy, he made a mistake starting at the 528th decimal place. 从历史上看,William Shanks在1873年将pi发布到小数点后707位。可怜的家伙,他从小数点后第528位开始犯了一个错误。

Very interestingly, in 1995 an algorithm was published that had the property that would directly calculate the nth digit (base 16) of pi without having to calculate all the previous digits ! 非常有趣的是,在1995 年发布一种算法该算法具有直接计算pi的第n位(基数16) 而不必计算所有先前数字的属性!

Finally, I hope your initial algorithm wasn't pi/4 = 1 - 1/3 + 1/5 - 1/7 + ... That may be the simplest to program, but it's also one of the slowest ways to do so. 最后,我希望你的初始算法不是pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...这可能是最简单的编程,但它也是最慢的方法之一。 Check out the pi article on Wikipedia for faster approaches. 查看维基百科上的pi文章,了解更快的方法。


#6楼

You could try computing sin(pi/2) (or cos(pi/2) for that matter) using the (fairly) quickly converging power series for sin and cos. 您可以尝试使用(相当)快速收敛的sin和cos幂级数来计算sin(pi/2) (或cos(pi/2) (Even better: use various doubling formulas to compute nearer x=0 for faster convergence.) (甚至更好:使用各种加倍公式来计算更近的x=0以便更快收敛。)

BTW, better than using series for tan(x) is, with computing say cos(x) as a black box (eg you could use taylor series as above) is to do root finding via Newton. 顺便说一句,比使用tan(x)系列更好,计算上说cos(x)是一个黑盒子(例如你可以使用上面的泰勒系列)是通过牛顿进行根找到。 There certainly are better algorithms out there, but if you don't want to verify tons of digits this should suffice (and it's not that tricky to implement, and you only need a bit of calculus to understand why it works.) 当然有更好的算法,但如果你不想验证大量的数字,这应该足够了(实现并不是那么棘手,你只需要一些微积分来理解它的工作原理。)

发布了0 篇原创文章 · 获赞 137 · 访问量 84万+

猜你喜欢

转载自blog.csdn.net/xfxf996/article/details/105435858