translated

This commit is contained in:
geekpi 2020-08-07 08:49:23 +08:00
parent f8c3b7d123
commit 608deb34a8
2 changed files with 173 additions and 175 deletions

View File

@ -1,175 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Do math in the Linux shell with GNU bc)
[#]: via: (https://opensource.com/article/20/7/bc-math)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
Do math in the Linux shell with GNU bc
======
Do better math in the shell with bc, a mathematical language for
advanced calculation.
![old school calculator][1]
Most [POSIX][2] systems come with [GNU bc][3], an arbitrary precision numeric processing language. Its syntax is similar to C, but it also supports interactive execution of statements and processing data from standard in (`stdin`). For that reason, it's often the answer to the question, "How do I do math in the Linux shell?" This style of response is common online:
```
$ echo "1+1" | bc
2
```
While that's perfectly valid, few users argue it's elegant compared to something more intuitive, such as:
```
$ 1+1  #this does not work
2
```
The interactive mode is a little easier:
```
$ bc
1+1
2
quit
$
```
But interactive modes don't always fit the desired and intuitive workflow of something simple, like just typing in the calculation you want. For this, I suggest Bluebat's [calculator in pure Bash][4].
What bc actually provides is a mathematical language for advanced calculation.
### Advanced functions with mathlib
On its own, `bc` provides basic math functions. You can test them in the interactive mode:
```
$ bc
3^2
9
(3^2)*(9)/3
27
```
Use the `--mathlib` option to gain advanced functions, including sine, cosine, tangent, and more. In the interactive mode, you can test some of them. Here's the cosine of 90:
```
c(90)
-.44807361612917015236
```
The sine of 9:
```
s(9)
.41211848524175656975
```
### Creating your own bc functions
You can also create your own functions in bc. Function definitions start with the `define` keyword and are enclosed with braces. Here is a simple function entered into an interactive session that returns whatever number it's given:
```
$ bc
define echo(n) {
  return (n);
}
```
In the same interactive session, test it out:
```
echo(2)
2
echo(-2)
-2
```
### If statements in bc
The bc language also has a variety of control statements, the simplest of which is if/else. The syntax may appear familiar at first glance, but there are subtleties in how braces are handled. Note that the _else_ clause of an if-statement is enclosed in braces, while the _then_ clause is not, but both are terminated with a semicolon. Here's a function to find the absolute value of a number _n_:
```
define abso(n) {
  if ( n > 0 ) return (n);
  { return (-n); }
}
```
In the same interactive session, test it out:
```
abso(-5)
5
abso(5)
5
```
### Importing data into bc
Working in an interactive session is tolerable for quick calculations and experimentation, but you lose your data when you quit, and it's difficult to edit when you make mistakes. Fortunately, bc can load variables and functions from external files.
Here's a file containing two variables (`sol` and `foo`) and a custom `abso` function to find an absolute value:
```
sol=299792458
foo=42
define abso(n) {
  if ( n > 0 ) return (n);
  { return (-n); }
}
```
Save this into a file called `bcvars.bc`, so you can import it into a bc interactive session:
```
$ bc bcvars.bc
foo
42
sol
299792458
abso(-23)
23
```
### Power-up your math with bc
The bc language is relatively simple, provided you know enough math to construct the equation for whatever you're trying to accomplish. While bc by default provides useful basic functions and allows you to create your own, you can reduce your workload by standing on the shoulders of giants. Files loaded with new functions, both for mathematic fundamentals as well as for specific tasks (for instance, calculating compound interest), are available from [GNU bc page][5], and [full documentation][6] for bc is available.
If you're interested in doing better math in a shell, try bc. It won't turn you into a mathematics genius, but it just might make it easier to become one.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/7/bc-math
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/math_money_financial_calculator_colors.jpg?itok=_yEVTST1 (old school calculator)
[2]: https://opensource.com/article/19/7/what-posix-richard-stallman-explains
[3]: https://www.gnu.org/software/bc/
[4]: https://raw.githubusercontent.com/bluebat/.bash/master/bashbc.sh
[5]: http://phodd.net/gnu-bc/
[6]: https://www.gnu.org/software/bc/manual/html_mono/bc.html

View File

@ -0,0 +1,173 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Do math in the Linux shell with GNU bc)
[#]: via: (https://opensource.com/article/20/7/bc-math)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
使用 GNU bc 在 Linux Shell 中进行数学运算
======
在 shell 中使用 bc 更好地做算数,它是一种用于高级计算的数学语言
![old school calculator][1]
大多数 [POSIX][2] 系统带有 [GNU bc][3],这是一种任意精度的数字处理语言。它的语法类似于 C但是它也支持交互式执行语句和处理来自标准输入`stdin`)的数据。因此,它通常是以下问题的答案:“我如何在 Linux shell 中进行数学运算?”这种回应方式在网上很常见:
```
$ echo "1+1" | bc
2
```
尽管这是完全正确的,但很少有用户认为,与更直观的方式相比,它很优雅,例如:
```
$ 1+1  #this does not work
2
```
交互模式要容易一些:
```
$ bc
1+1
2
quit
$
```
但是交互模式并不总是适合简单计算想要的直观工作流,比如直接输入你想要的计算。因此我推荐 Bluebat 的[纯 Bash 计算器][4]。
bc 实际上提供了一种用于高级计算的数学语言。
### 含高级函数的 mathlib
`bc` 本身提供了基本的数学函数。你可以在交互式模式下测试它们:
```
$ bc
3^2
9
(3^2)*(9)/3
27
```
使用 `--mathlib` 选项获取高级函数,包括正弦、余弦、正切等。在交互式模式下,你可以测试其中一些。下面是 90 度的余弦:
```
c(90)
-.44807361612917015236
```
9 的正弦:
```
s(9)
.41211848524175656975
```
### 创建你自己的 bc 函数
你还可以在 bc 中创建自己的函数。函数定义以 `define` 关键字开始,并用大括号括起来。下面是一个输入到交互式会话中的简单函数,它返回给它的任意数字:
```
$ bc
define echo(n) {
  return (n);
}
```
在同一个交互式会话中,测试一下:
```
echo(2)
2
echo(-2)
-2
```
### bc 中的 if 语句
bc 语言还有各种控制语句,其中最简单的是 if/else。语法乍一看可能很熟悉但在如何处理大括号方面有些不同。请注意if 语句的 _else_ 子句包含在大括号中,而 _then_ 子句不是,但两者都用分号终止。下面是一个返回数字 _n_ 的绝对值的函数:
```
define abso(n) {
  if ( n > 0 ) return (n);
  { return (-n); }
}
```
在同一个交互式会话中,测试一下:
```
abso(-5)
5
abso(5)
5
```
### 将数据导入 bc
使用交互式会话对于快速计算和实验是可以容忍的但在退出时会丢失数据并且在出错时很难编辑。幸运的是bc 可以从外部文件加载变量和函数。
下面是一个包含两个变量(`sol` 和 `foo`)的文件,以及一个用于查找绝对值的自定义 `abso` 函数:
```
sol=299792458
foo=42
define abso(n) {
  if ( n > 0 ) return (n);
  { return (-n); }
}
```
将它保存到名为 `bcvars.bc` 的文件中,以便导入 bc 交互式会话:
```
$ bc bcvars.bc
foo
42
sol
299792458
abso(-23)
23
```
### 使用 bc 助力你的数学
bc 语言相对简单,前提是你知道足够的数学知识来构造任何你想完成的方程。虽然 bc 默认提供了有用的基本函数,并允许你创建自己的函数,但你可以通过站在巨人的肩膀上来减少工作量。加载了用于数学基础知识和特定任务(例如,计算复利)的新函数的文件可从 [GNU bc 页][5]获得,同时也可获得 bc [完整文档][6]。
如果你有兴趣在 shell 里更好地处理数学,试试 bc 。它不会使你成为数学天才,但它可能会让过程更简单。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/7/bc-math
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/math_money_financial_calculator_colors.jpg?itok=_yEVTST1 (old school calculator)
[2]: https://opensource.com/article/19/7/what-posix-richard-stallman-explains
[3]: https://www.gnu.org/software/bc/
[4]: https://raw.githubusercontent.com/bluebat/.bash/master/bashbc.sh
[5]: http://phodd.net/gnu-bc/
[6]: https://www.gnu.org/software/bc/manual/html_mono/bc.html