mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
translated
This commit is contained in:
parent
219885d58b
commit
7670b98695
@ -1,116 +0,0 @@
|
|||||||
JonathanKang is translating
|
|
||||||
What is a good command-line calculator on Linux
|
|
||||||
================================================================================
|
|
||||||
Every modern Linux desktop distribution comes with a default GUI-based calculator app. On the other hand, if your workspace is full of terminal windows, and you would rather crunch some numbers within one of those terminals quickly, you are probably looking for a **command-line calculator**. In this category, [GNU bc][1] (short for "basic calculator") is a hard to beat one. While there are many command-line calculators available on Linux, I think GNU bc is hands-down the most powerful and useful.
|
|
||||||
|
|
||||||
Predating the GNU era, bc is actually a historically famous arbitrary precision calculator language, with its first implementation dating back to the old Unix days in 1970s. Initially bc was a better known as a programming language whose syntax is similar to C language. Over time the original bc evolved into POSIX bc, and then finally GNU bc of today.
|
|
||||||
|
|
||||||
### Features of GNU bc ###
|
|
||||||
|
|
||||||
Today's GNU bc is a result of many enhancements of earlier implementations of bc, and now it comes standard on all major GNU/Linux distros. It supports standard arithmetic operators with arbitrary precision numbers, and multiple numeric base (e.g., binary, decimal hexadecimal) of input and output.
|
|
||||||
|
|
||||||
If you are familiar with C language, you will see that the same or similar mathematical operators are used in bc. Some of supported operators include arithmetic (+,-,*,/,%,++,--), comparison (<,>,==,!=,<=,>=), logical (!,&&,||), bitwise (&,|,^,~,<<,>>), compound assignment (+=,-=,*=,/=,%=,&=,|=,^=,&&=,||=,<<=,>>=) operators. bc comes with many useful built-in functions such as square root, sine, cosine, arctangent, natural logarithm, exponential, etc.
|
|
||||||
|
|
||||||
### How to Use GNU bc ###
|
|
||||||
|
|
||||||
As a command-line calculator, possible use cases of GNU bc are virtually limitless. In this tutorial, I am going to describe a few popular features of bc command. For a complete manual, refer to the [official source][2].
|
|
||||||
|
|
||||||
Unless you have a pre-written bc script, you typically run bc in interactive mode, where any typed statement or expression terminated with a newline is interpreted and executed on the spot. Simply type the following to enter an interactive bc session. To quit a session, type 'quit' and press Enter.
|
|
||||||
|
|
||||||
$ bc
|
|
||||||
|
|
||||||
![](https://farm4.staticflickr.com/3939/15403325480_d0db97d427_z.jpg)
|
|
||||||
|
|
||||||
The examples presented in the rest of the tutorial are supposed to be typed inside a bc session.
|
|
||||||
|
|
||||||
### Type expressions ###
|
|
||||||
|
|
||||||
To calculate an arithmatic expression, simply type the expression at the blinking cursor, and press Enter. If you want, you can store an intermediate result to a variable, then access the variable in other expressions.
|
|
||||||
|
|
||||||
![](https://farm6.staticflickr.com/5604/15403325460_b004b3f8da_o.png)
|
|
||||||
|
|
||||||
Within a given session, bc maintains a unlimited history of previously typed lines. Simply use UP arrow key to retrieve previously typed lines. If you want to limit the number of lines to keep in the history, assign that number to a special variable named history. By default the variable is set to -1, meaning "unlimited."
|
|
||||||
|
|
||||||
### Switch input/output base ###
|
|
||||||
|
|
||||||
Often times you want to type input expressions and display results in binary or hexadecimal formats. For that, bc allows you switch the numeric base of input or output numbers. Input and output bases are stored in ibase and obase, respectively. The default value of these special variables is 10, and valid values are 2 through 16 (or the value of BC_BASE_MAX environment variable in case of obase). To switch numeric base, all you have to do is to change the values of ibase and obase. For example, here are examples of summing up two hexadecimal/binary numbers:
|
|
||||||
|
|
||||||
![](https://farm6.staticflickr.com/5604/15402320019_f01325f199_z.jpg)
|
|
||||||
|
|
||||||
Note that I specify obase=16 before ibase=16, not vice versa. That is because if I specified ibase=16 first, the subsequent obase=16 statement would be interpreted as assigning 16 in base 16 to obase (i.e., 22 in decimal), which is not what we want.
|
|
||||||
|
|
||||||
### Adjust precision ###
|
|
||||||
|
|
||||||
In bc, the precision of numbers is stored in a special variable named scale. This variable represents the number of decimal digits after the decimal point. By default, scale is set to 0, which means that all numbers and results and truncated/stored in integers. To adjust the default precision, all you have to do is to change the value of scale variable.
|
|
||||||
|
|
||||||
scale=4
|
|
||||||
|
|
||||||
![](https://farm6.staticflickr.com/5597/15586279541_211312597b.jpg)
|
|
||||||
|
|
||||||
### Use built-in functions ###
|
|
||||||
|
|
||||||
Beyond simple arithmatic operations, GNU bc offers a wide range of advanced mathematical functions built-in, via an external math library. To use those functions, launch bc with "-l" option from the command line.
|
|
||||||
|
|
||||||
Some of these built-in functions are illustrated here.
|
|
||||||
|
|
||||||
Square root of N:
|
|
||||||
|
|
||||||
sqrt(N)
|
|
||||||
|
|
||||||
Sine of X (X is in radians):
|
|
||||||
|
|
||||||
s(X)
|
|
||||||
|
|
||||||
Cosine of X (X is in radian):
|
|
||||||
|
|
||||||
c(X)
|
|
||||||
|
|
||||||
Arctangent of X (The returned value is in radian):
|
|
||||||
|
|
||||||
a(X)
|
|
||||||
|
|
||||||
Natural logarithm of X:
|
|
||||||
|
|
||||||
l(X)
|
|
||||||
|
|
||||||
Exponential function of X:
|
|
||||||
|
|
||||||
e(X)
|
|
||||||
|
|
||||||
### Other goodies as a language ###
|
|
||||||
|
|
||||||
As a full-blow calculator language, GNU bc supports simple statements (e.g., variable assignment, break, return), compound statements (e.g., if, while, for loop), and custom function definitions. I am not going to cover the details of these features, but you can easily learn how to use them from the [official manual][2]. Here is a very simple function definition example:
|
|
||||||
|
|
||||||
define dummy(x){
|
|
||||||
return(x * x);
|
|
||||||
}
|
|
||||||
dummy(9)
|
|
||||||
81
|
|
||||||
dummy(4)
|
|
||||||
16
|
|
||||||
|
|
||||||
### Use GNU bc Non-interactively ###
|
|
||||||
|
|
||||||
So far we have used bc within an interactive session. However, quite popular use cases of bc in fact involve running bc within a shell script non-interactively. In this case, you can send input to bc using echo through a pipe. For example:
|
|
||||||
|
|
||||||
$ echo "40*5" | bc
|
|
||||||
$ echo "scale=4; 10/3" | bc
|
|
||||||
$ echo "obase=16; ibase=2; 11101101101100010" | bc
|
|
||||||
|
|
||||||
![](https://farm4.staticflickr.com/3943/15565252976_f50f453c7f_z.jpg)
|
|
||||||
|
|
||||||
To conclude, GNU bc is a powerful and versatile command-line calculator that really lives up to your expectation. Preloaded on all modern Linux distributions, bc can make your number crunching tasks much easy to handle without leaving your terminals. For that, GNU bc should definitely be in your productivity toolset.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: http://xmodulo.com/command-line-calculator-linux.html
|
|
||||||
|
|
||||||
作者:[Dan Nanni][a]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]:http://xmodulo.com/author/nanni
|
|
||||||
[1]:http://www.gnu.org/software/bc/
|
|
||||||
[2]:https://www.gnu.org/software/bc/manual/bc.html
|
|
@ -0,0 +1,115 @@
|
|||||||
|
怎么样称得上是Linux下优秀的命令行计算器
|
||||||
|
================================================================================
|
||||||
|
每个现代的Linux桌面发行版都预装着一个带有图形界面的计算器程序。不过如果你的工作区中全是命令行窗口,那么你一定会在其中的一个命令行窗口中处理一些数字相关的问题。或许你在寻找一款基于命令行的计算器程序。如果是这样的话,[GNU bc][1](“basic calculator”的缩写)会是你不二的选择。当然Linux下又很多基于命令行的计算器应用,我认为GNU bc是功能最强大和最有用的。
|
||||||
|
|
||||||
|
在GNU时代之前,bc实际上是一个著名的精密计算语言。它的诞生要追溯到70年代的Unix时期了。最初bc作为一个语法和C语言相似的编程语言而著名。随着时间的改变,最开始的bc演化成POSIX bc,最后变成了今天的GNU bc。
|
||||||
|
|
||||||
|
### GNU bc的特性 ###
|
||||||
|
|
||||||
|
现在的GNU bc是早期bc经过若干次改进和功能增强的结果。目前它被所有的主流GNU/Linux发行版所收纳。GNU bc支持高精度数字和多种数值类型(例如二进制、十进制、十六进制)的输入输出。
|
||||||
|
|
||||||
|
如果你对C语言很熟悉的话,你会发现bc使用了和C语言一样或相似的算术操作符。受支持的操作符包括算术运算符(+,-,*,/,%,++,--)、比较运算符(<,>,==,!=,<=,>=)、逻辑运算符(!,&&,||)、位运算符(&,|,^,~,<<,>>)和复合赋值运算符(+=,-=,*=,/=,%=,&=,|=,^=,&&=,||=,<<=,>>=)。bc内置了很多有用的函数,像是平方根、正弦、余弦、反正弦、自然对数、指数等。
|
||||||
|
|
||||||
|
### 如何使用GNU bc ###
|
||||||
|
|
||||||
|
作为一个基于命令行的计算器,GNU bc的使用是没有限制的。在本文中,我会向大家介绍bc命令的几个常用的特性。如果你想要更加详细的指导,你可以查阅[官方指南][2]。
|
||||||
|
|
||||||
|
如果你没有一个预先写好的bc脚本,那么你需要在交互模式下运行bc。在这种模式下,你输入的以回车结束的任何声明或者表达式会被立刻计算出结果。你需要输入以下命令来进入bc的交互界面。如果向退出bc,你可以输入'quit'并且按回车。
|
||||||
|
|
||||||
|
$ bc
|
||||||
|
|
||||||
|
![](https://farm4.staticflickr.com/3939/15403325480_d0db97d427_z.jpg)
|
||||||
|
|
||||||
|
本文下面展示的例子应该在bc交互界面中输入。
|
||||||
|
|
||||||
|
### 输入表达式 ###
|
||||||
|
|
||||||
|
如果想要计算一个算术表达式,我们可以在闪烁的光标处输入该表达式,然后按回车确认。你也可以将该结果存储到一个变量中,然后在其他表达式中使用该变量。
|
||||||
|
|
||||||
|
![](https://farm6.staticflickr.com/5604/15403325460_b004b3f8da_o.png)
|
||||||
|
|
||||||
|
在一个bc的交互界面中,存在没有个数限制的命令历史记录。使用上方向键来查看之前输入的命令。如果你想限制历史记录保存的命令数量,你可以将一个名为history的特殊变量设置成你希望的数值。该变量默认为-1,也就是“历史记录数量没有限制”。
|
||||||
|
|
||||||
|
### 输入输出进制切换 ###
|
||||||
|
|
||||||
|
经常会发生的是,你输入一个表达式并且想使用二进制或者十六进制来显示结果。bc允许你在输入输出数字的进制间转换。输入和输出的数系基分别存储在ibase和obase变量中,默认值为10,有效的数值是2到16(或者环境变量BC_BASE_MAX的值).你只需要更改ibase和obase的值就可以在不同进制之间转换了。下面是一个求两个十六进制/二进制数和的例子:
|
||||||
|
|
||||||
|
![](https://farm6.staticflickr.com/5604/15402320019_f01325f199_z.jpg)
|
||||||
|
|
||||||
|
需要注意的是,我有意地将obase=16放到了ibase=16前面,反过来则是不可以的。这个是因为如果我先输入ibase=16,那么随后输入的obase=16中的16会被认为是16进制的数字,也就是十进制的22。当然这个不是我们所期望的。
|
||||||
|
|
||||||
|
### 调整精度 ###
|
||||||
|
|
||||||
|
在bc中,数字的精度存储在一个名为scale的特殊变量中。该变量表示小数点后数字的个数。scale默认为0,意味着所有的数字和结果以整数形式储存。你可以通过改变scale这个特殊变量的值,来调整数值的精度。
|
||||||
|
|
||||||
|
scale=4
|
||||||
|
|
||||||
|
![](https://farm6.staticflickr.com/5597/15586279541_211312597b.jpg)
|
||||||
|
|
||||||
|
### 使用内置函数 ###
|
||||||
|
|
||||||
|
除了简单的算术操作符,GNU bc还通过外部的数学函数库来提供许多高级的数学函数。你可以在命令行界面使用“-l”选项来打开bc。
|
||||||
|
|
||||||
|
这里描述了一些内置的函数。
|
||||||
|
|
||||||
|
N的二次方根:
|
||||||
|
|
||||||
|
sqrt(N)
|
||||||
|
|
||||||
|
X的正弦(X是弧度):
|
||||||
|
|
||||||
|
s(X)
|
||||||
|
|
||||||
|
X的余弦(X是弧度):
|
||||||
|
|
||||||
|
c(X)
|
||||||
|
|
||||||
|
X的反正弦(返回值是弧度):
|
||||||
|
|
||||||
|
a(X)
|
||||||
|
|
||||||
|
X的自然对数:
|
||||||
|
|
||||||
|
l(X)
|
||||||
|
|
||||||
|
X的指数对数:
|
||||||
|
|
||||||
|
e(X)
|
||||||
|
|
||||||
|
### Other goodies as a language ###
|
||||||
|
|
||||||
|
作为一个计算语言,GNU bc支持简单的声明(变量赋值、中断、返回等)、复合语句(if、while、for loop等)和自定义函数。在这里我不会涉及到这些特性的细节,不过你可以通过[官方指南][2]来学习如何使用这些特性。下面是一个简单的函数示例:
|
||||||
|
|
||||||
|
define dummy(x){
|
||||||
|
return(x * x);
|
||||||
|
}
|
||||||
|
dummy(9)
|
||||||
|
81
|
||||||
|
dummy(4)
|
||||||
|
16
|
||||||
|
|
||||||
|
### 在非交互界面下使用GNU bc ###
|
||||||
|
|
||||||
|
到目前为止,我们一直在交互界面下使用bc。不过更加流行的使用bc的方法是在没有交互界面的脚本中运行bc。这种情况下,你可以使用echo命令并且借助管道来向bc发送输入内容。例如:
|
||||||
|
|
||||||
|
$ echo "40*5" | bc
|
||||||
|
$ echo "scale=4; 10/3" | bc
|
||||||
|
$ echo "obase=16; ibase=2; 11101101101100010" | bc
|
||||||
|
|
||||||
|
![](https://farm4.staticflickr.com/3943/15565252976_f50f453c7f_z.jpg)
|
||||||
|
|
||||||
|
总结以下,GNU bc是一款强大并且通用的基于命令行的计算器应用,因此它绝对不会让你失望。它预装在所有的现代Linux发行版中,bc可以让你不用离开命令行就可以进行高效的数学计算。所以,GNU bc一定会是你的最爱。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: http://xmodulo.com/command-line-calculator-linux.html
|
||||||
|
|
||||||
|
作者:[Dan Nanni][a]
|
||||||
|
译者:[JonathanKang](https://github.com/JonathanKang)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]:http://xmodulo.com/author/nanni
|
||||||
|
[1]:http://www.gnu.org/software/bc/
|
||||||
|
[2]:https://www.gnu.org/software/bc/manual/bc.html
|
Loading…
Reference in New Issue
Block a user