linux的bc计算器

bc 命令:
     bc 命令是用于命令行计算器。 它类似基本的计算器。 使用这个计算器可以做基本的数学运算。

语法:
  语法是
     bc [命令开关]

命令开关:
     -c 仅通过编译。 bc命令的输出被发送到标准输出。
-l 定义数学函数并且初始化值为20,取代默认值0。
filename 文件名,它包含用于计算的计算器命令,这不是必须的命令。
示例:
    
1)bc
bc在默认的情况下是个交互式的指令。在bc工作环境下,可以使用以下计算符号:
+ 加法
- 减法
* 乘法
/ 除法
^ 指数
% 余数
如:
# bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
3+6            <=加法
9
4+2*3          <=加法、乘法
10
(4+2)*3        <=加法、乘法(优先)
18
4*6/8          <=乘法、除法
3
10^3           <=指数
1000
18%5           <=余数
3+4;5*2;5^2;18/4      <=一行输入多个计算,用;相隔。
7
10
25
4
quit           <=退出

# bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=3         <=设小数位
1/3
.333
quit
以上是交互的计算,那到也可以不进行交互而直接计算出结果。
A.用echo和|法,如:
# echo "(6+3)*2" |bc
18
# echo 15/4 |bc
3
# echo "scale=2;15/4" |bc
3.75
# echo "3+4;5*2;5^2;18/4" |bc
7
10
25
4
另外,bc除了scale来设定小数位之外,还有ibase和obase来其它进制的运算。
如:
//将16进制的A7输出为10进制, 注意,英文只能大写
# echo "ibase=16;A7" |bc
167
//将2进制的11111111转成10进制
# echo "ibase=2;11111111" |bc
255
//输入为16进制,输出为2进制
# echo "ibase=16;obase=2;B5-A4" |bc
10001

对于bc还有补充,在bc --help中还可以发现:bc后可以接文件名。如:
# more calc.txt
3+2
4+5
8*2
10/4
# bc calc.txt
5
9
16
2



MATH LIBRARY
       If bc is invoked with the -l option, a math library is preloaded and the default  scale  is  set  to  20.
       The  math  functions  will  calculate their results to the scale set at the time of their call.  The math
       library defines the following functions:

       s (x)  The sine of x, x is in radians.    正玄函数
c (x)  The cosine of x, x is in radians.  余玄函数

       a (x)  The arctangent of x, arctangent returns radians. 反正切函数

       l (x)  The natural logarithm of x.  log函数(以2为底)

       e (x)  The exponential function of raising e to the value x.  e的指数函数

       j (n,x)
              The bessel function of integer order n of x.   贝塞尔函数


PS: echo "scale=100; a(1)*4" | bc -l  (计算圆周率)

我试了,很神奇,可以计算圆周率到100位,而且速度很快

猜你喜欢

转载自1050113483.iteye.com/blog/2258539
今日推荐