2019-04-21 11:50:48 +08:00

41 lines
1.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="bc">bc</h1>
<p>算术操作精密运算工具</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>bc命令</strong> 是一种支持任意精度的交互执行的计算器语言。bash内置了对整数四则运算的支持但是并不支持浮点运算而bc命令可以很方便的进行浮点运算当然整数运算也不再话下。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">bc(选项)(参数)</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">-i强制进入交互式模式
-l定义使用的标准数学库
-w对POSIX bc的扩展给出警告信息
-q不打印正常的GNU bc环境信息
-v显示指令版本信息
-h显示指令的帮助信息。</code></pre>
<h3 id="参数">参数</h3>
<p>文件:指定包含计算任务的文件。</p>
<h3 id="实例">实例</h3>
<p>算术操作高级运算bc命令它可以执行浮点运算和一些高级函数</p>
<pre><code class="language-bash">echo &quot;1.212*3&quot; | bc
3.636
</code></pre>
<p>设定小数精度(数值范围)</p>
<pre><code class="language-bash">echo &quot;scale=2;3/8&quot; | bc
0.37
</code></pre>
<p>参数<code>scale=2</code>是将bc输出结果的小数位设置为2位。</p>
<p>进制转换</p>
<pre><code class="language-bash">#!/bin/bash
abc=192
echo &quot;obase=2;$abc&quot; | bc
</code></pre>
<p>执行结果为11000000这是用bc将十进制转换成二进制。</p>
<pre><code class="language-bash">#!/bin/bash
abc=11000000
echo &quot;obase=10;ibase=2;$abc&quot; | bc
</code></pre>
<p>执行结果为192这是用bc将二进制转换为十进制。</p>
<p>计算平方和平方根:</p>
<pre><code class="language-bash">echo &quot;10^10&quot; | bc
echo &quot;sqrt(100)&quot; | bc</code></pre>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->