Home > System Tutorial > LINUX > body text

Perform math calculations using the Linux command line

王林
Release: 2024-01-15 13:39:05
forward
848 people have browsed it

If you need a calculator in a graphical desktop environment, you may just click all the way to find a calculator. For example, Fedora Workstation already includes a tool called Calculator. It has several different operating modes, for example, you can perform complex mathematical operations or financial operations. But, did you know that the command line also provides a similar tool called bc?

bc The tool can provide you with the functions you would expect from a scientific calculator, a financial calculator or a simple calculator. Additionally, it can be scripted from the command line if desired. This allows you to use it in shell scripts when you need to do complex math.

Because bc is also used by other system software, such as the CUPS print service, it may already be installed on your Fedora system. You can use the following command to check:

dnf list installed bc
Copy after login

If for some reason you don't see it in the output of the above command, you can use the following command to install it:

sudo dnf install bc
Copy after login
Use bc to do some simple mathematical operations

One way to use bc is to enter its own shell. There you can do many calculations row by row. When you type bc, the first thing that appears is a warning about this program:

$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
Copy after login

Now you can enter calculations or commands one per line:

1+1
Copy after login

bc will answer the answer to the above calculation formula:

2
Copy after login

You can also execute other commands here. You can use addition ( ), subtraction (-), multiplication (*), division (/), parentheses, exponent symbol (^), etc. Please note that bc also follows all conventional operation rules, such as the order of operations. You can try the following example:

(4+7)*2
4+7*2
Copy after login

To exit bc, you can send the "input end" signal to bc through the key combination Ctrl D.

Another way to use bc is to use the echo command to pass an expression or command. The following example is the "Hello, world" example in the calculator, using the shell's pipe function (|) to pass the output of echo into bc:

echo '1+1' | bc
Copy after login

Using shell pipes, you can send more than one operation. You need to use semicolons to separate different operations. The results will be returned in separate rows.

echo '1+1; 2+2' | bc
Copy after login
Accuracy

In some calculations, bc will use the concept of precision, that is, the number of digits after the decimal point. The default precision is 0. Division operations always use the precision setting. Therefore, if you do not set the precision, it may bring unexpected answers:

echo '3/2' | bc
echo 'scale=3; 3/2' | bc
Copy after login

Multiplication uses a more complex precision selection mechanism:

echo '3*2' | bc
echo '3*2.0' | bc
Copy after login

At the same time, the related operations of addition and subtraction are similar:

echo '7-4.15' | bc
Copy after login
Other base systems

Another useful feature of bc is the ability to use other counting systems besides decimal. For example, you can easily do hexadecimal or binary math operations. You can use the ibase and obase commands to set the input and output base systems respectively. Keep in mind that once you use ibase, any numbers you enter thereafter will be considered to be in the newly defined base system.

To convert or perform operations from hexadecimal numbers to decimal numbers, you can use commands similar to the following. Please note that hexadecimal numbers greater than 9 must be in uppercase (A-F):

echo 'ibase=16; A42F' | bc
echo 'ibase=16; 5F72+C39B' | bc
Copy after login

To make the result a hexadecimal number, you need to set obase:

echo 'obase=16; ibase=16; 5F72+C39B' | bc
Copy after login

下面是一个小技巧。假如你在 shell 中做这些十六进制运算,怎样才能使得输入重新为十进制数呢?答案是使用 ibase 命令,但你必须设定它为在当前进制中与十进制中的 10 等价的值。例如,假如 ibase 被设定为十六进制,你需要输入:

ibase=A
Copy after login

一旦你执行了上面的命令,所有输入的数字都将是十进制的了,接着你便可以输入 obase=10 来重置输出的进制系统。

结论

上面所提到的只是 bc 所能做到的基础。它还允许你为某些复杂的运算和程序定义函数、变量和循环结构。你可以在你的系统中将这些程序保存为文本文件以便你在需要的时候使用。你还可以在网上找到更多的资源,它们提供了更多的例子以及额外的函数库。快乐地计算吧!


The above is the detailed content of Perform math calculations using the Linux command line. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template