博客园  :: 首页  :: 管理

关于Linux中使用bc命令计算圆周率

Posted on 2020-12-19 20:28  520_1351  阅读(3005)  评论(0编辑  收藏  举报

Linux系统中,我们可以安装bc计算器用来计算pi值(π圆周率)

在玩的同时,这可以从某些方面反映出一个CPU的运算能力,在命令最前加上time即可

如果系统中没有bc命令,需要先进行安装:yum install bc -y

然后再执行如下命令,即可计算π圆周率,至于多少位、可以自己指定,这里只计算到30位,如下:

[root@qq_5201351 ~]# echo "scale=30; a(1)*4" | bc -l
3.141592653589793238462643383276

# 命令的说明如下:
-l  --mathlib    use the predefined math routines
a(x)             The arctangent of x, arctangent returns radians
sacle=x          Define  the number of digits after the decimal point

原理:4*a(1)调用了反正切函数,由三角函数知道1的反正切是π/4, π=4*π/4

 

 

尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/14160929.html