目录

Bash脚本之计算圆周率

目录

通过Bash脚本计算圆周率,可以简单地测试CPU性能。

jerome@ubuntu:~$ time echo "scale=100; a(1)*4" | bc -l
3.141592653589793238462643383279502884197169399375105820974944592307\
8164062862089986280348253421170676

real    0m0.052s
user    0m0.011s
sys     0m0.027s

通过 man bc 可以查看 -l 参数的意思是:

-l, --mathlib
       Define the standard math library.

而函数 a(x) 则是:

a (x)  The arctangent of x, arctangent returns radians.

所以,这里是利用了 bc 命令中的反正切函数(arctan),因为在反三角函数中:

tan(π/4) = 1  ==>  π = 4 * arctan(1)

scale 是小数点后的位数(即小数精度):

scale ( expression )
       The value of the scale function is the number of digits after the decimal point in the expression.