如何使用 Bash 命令计算 pi

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23524661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:24:47  来源:igfitidea点击:

How can I calculate pi using Bash command

bashshellseqbc

提问by poorvank

I am learning bash scripting. While exploring the math functions i am came across a command which calculated the value of pi.

我正在学习 bash 脚本。在探索数学函数时,我遇到了一个计算 pi 值的命令。

seq -f '4/%g' 1 2 99999 | paste -sd-+ | bc -l

Although i understand how the basic seq command works, I am unable to understand how does the above command works. Can anybody please clarify how does it work.?

虽然我了解基本 seq 命令的工作原理,但我无法理解上述命令的工作原理。任何人都可以请澄清它是如何工作的。?

回答by devnull

This calculates the value of π using Gregory–Leibniz series:

这使用 Gregory-Leibniz 级数计算 π 的值:

enter image description here

在此处输入图片说明

seq -f '4/%g' 1 2 99999generates the fractions:

seq -f '4/%g' 1 2 99999生成分数:

4/1
4/3
4/5
4/7
4/9
4/11
4/13
4/15
4/17
4/19

The paste pipeline paste -sd-+combines those with alternate delimiters -and +.

粘贴管道paste -sd-+将那些与备用分隔符-+.

Finally, bc -lperforms the arithmetic to give the result.

最后,bc -l执行算术运算以给出结果。



EDIT: As noted in the comment, this sequence converges very slowly. Machin's formulahas a significantly higher rate of convergence:

编辑:正如评论中所指出,这个序列收敛得非常缓慢。 Machin 公式具有明显更高的收敛速度:

enter image description here

在此处输入图片说明

Using the same expansion for tan-1(x):

对 tan -1(x)使用相同的展开式:

enter image description here

在此处输入图片说明

to compute π, we can see that it produces the correct value to 50 digits1using just the first 50 terms of the series:

为了计算 π,我们可以看到它仅使用系列的前 50 项就可以生成 50 位数字1的正确值:

$ { echo -n "scale=50;"; seq 1 2 100 | xargs -n1 -I{} echo '(16*(1/5)^{}/{}-4*(1/239)^{}/{})';} | paste -sd-+ | bc -l
3.14159265358979323846264338327950288419716939937510

With just 100 terms, the value of π is computed accurately to more than 100 digits:

只需 100 项,π 的值就可以精确计算到 100 多个数字:

$ { echo -n "scale=100;"; seq 1 2 200 | xargs -n1 -I{} echo '(16*(1/5)^{}/{}-4*(1/239)^{}/{})';} | paste -sd-+ | bc -l
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

1Pi

1圆周率

回答by James Scriven

Not a direct answer to your question about using seq, but pi can be easily computed using bc:

不是您有关 using 问题的直接答案seq,但可以使用pi 轻松计算bc

 echo "scale=1000; 4*a(1)" | bc -l

ais arctan, and this give pi to 1000 digits.

a是反正切,这给 pi 到 1000 位数字。

回答by Nathaniel Flath

seq -f 4 %g 1 2 99999 

Gives the data:

给出数据:

4/1
4/3 
4/5 
...
4/9999

The paste command takes this list and inserts a - between the first two, a + between the second two, etc (and puts it on one line, so):

paste 命令获取此列表并在前两个之间插入 - ,在后两个之间插入 + 等(并将其放在一行上,因此):

4/1-4/3+4/5-4/7......4/9999

Which is an approximation of pi. The 'bc' program calculates this and prints the value.

这是 pi 的近似值。'bc' 程序计算它并打印该值。