> For the complete documentation index, see [llms.txt](https://hezhiqiang.gitbook.io/linux/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hezhiqiang.gitbook.io/linux/ming-ling/bc.md).

# bc

算术操作精密运算工具

## 补充说明

**bc命令** 是一种支持任意精度的交互执行的计算器语言。bash内置了对整数四则运算的支持，但是并不支持浮点运算，而bc命令可以很方便的进行浮点运算，当然整数运算也不再话下。

### 语法

```
bc(选项)(参数)
```

### 选项

```
-i：强制进入交互式模式；
-l：定义使用的标准数学库；
-w：对POSIX bc的扩展给出警告信息；
-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
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hezhiqiang.gitbook.io/linux/ming-ling/bc.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
