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.

33 lines
1.1 KiB

5 years ago
# **bc**
7 years ago
6 years ago
## 说明
7 years ago
5 years ago
**bc命令** 是一种支持任意精度的交互执行的计算器语言。bash内置了对整数四则运算的支持但是并不支持浮点运算而bc命令可以
很方便的进行浮点运算,当然整数运算也不再话下
7 years ago
5 years ago
## 选项
7 years ago
5 years ago
```markdown
-i --interactive 强制进入交互式模式输入quit后回车退出此模式
-l --mathlib 定义使用的标准数学库
-q --quiet 不打印初始化信息
6 years ago
-s --standard non-standard bc constructs are errors
5 years ago
-w --warn warn about non-standard bc constructs
7 years ago
```
5 years ago
## 实例
7 years ago
5 years ago
```bash
echo "1.212*3" | bc # 算术操作高级运算bc命令它可以执行浮点运算和一些高级函数
echo "scale=2;3/8" | bc # 设定小数精度(数值范围)参数`scale=2`是将bc输出结果的小数位设置为2位
echo "obase=2;192" | bc # 进制转换,将数字192转换为2进制注意只能对数字进行进制转换,对应的进制有2,8,16,64
5 years ago
echo "$((0x2dec))" # 十六进制转十进制
echo "sqrt(1000/10) + 1" | bc -l
7 years ago
5 years ago
# 计算平方和平方根
7 years ago
echo "10^10" | bc
echo "sqrt(100)" | bc
5 years ago
```