You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
bc
|
|
|
|
|
===
|
|
|
|
|
|
|
|
|
|
算术操作精密运算工具
|
|
|
|
|
|
|
|
|
|
## 说明
|
|
|
|
|
|
|
|
|
|
**bc命令** 是一种支持任意精度的交互执行的计算器语言。bash内置了对整数四则运算的支持,但是并不支持浮点运算,而bc命令可以很方便的进行浮点运算,当然整数运算也不再话下。
|
|
|
|
|
|
|
|
|
|
### 语法
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
bc(选项)(参数)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
-i:强制进入交互式模式;输入quit后回车退出此模式;
|
|
|
|
|
-l:定义使用的标准数学库;
|
|
|
|
|
-w:对POSIX bc的扩展给出警告信息;
|
|
|
|
|
-s --standard non-standard bc constructs are errors
|
|
|
|
|
-q:不打印正常的GNU bc环境信息;
|
|
|
|
|
-v:显示指令版本信息;
|
|
|
|
|
-h:显示指令的帮助信息。
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 参数
|
|
|
|
|
|
|
|
|
|
文件:指定包含计算任务的文件。
|
|
|
|
|
|
|
|
|
|
### 实例
|
|
|
|
|
|
|
|
|
|
算术操作高级运算bc命令它可以执行浮点运算和一些高级函数:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
echo "1.212*3" | bc
|
|
|
|
|
3.636
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
设定小数精度(数值范围)
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
echo "scale=2;3/8" | bc
|
|
|
|
|
0.37
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
参数`scale=2`是将bc输出结果的小数位设置为2位。
|
|
|
|
|
|
|
|
|
|
进制转换
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
abc=192
|
|
|
|
|
echo "obase=2;$abc" | bc
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
执行结果为:11000000,这是用bc将十进制转换成二进制。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
abc=11000000
|
|
|
|
|
echo "obase=10;ibase=2;$abc" | bc
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
执行结果为:192,这是用bc将二进制转换为十进制。
|
|
|
|
|
|
|
|
|
|
计算平方和平方根:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
echo "10^10" | bc
|
|
|
|
|
echo "sqrt(100)" | bc
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|