Share a command line calculator - bc

Share a command-line calculator - bc

If you need a calculator in a graphical desktop environment, you might just have to click all the way to find one. For example, Fedora Workstation already includes a tool called Calculator. It has several different modes of operation, 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? The following Brothers (www.lampbrother.net) will give you an introduction:

The bc tool can provide you with functions that meet your expectations for scientific calculators, financial calculators or simple calculators. 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.

Since bc is also used for other system software, such as the CUPS print service, it may already be installed on your Fedora system. You can check it with this command:

dnf list installed bc
If for some reason you don't see it in the output of the above command, you can install it with this command:

sudo dnf install
bc do something with bc Simple Math 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 warning about this program appears:

$ 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'.
Now you can enter expressions or commands one by one per line:

1+1
bc will answer the above formula:

2
Here you can also execute other commands. You can use addition (+), subtraction (-), multiplication (*), division (/), parentheses, exponential sign (^), and more. Note that bc also follows all conventional rules of operation, such as the order of operations. You can try the following example:

(4+7)*2
4+7*2
To exit bc you can send the "end of input" signal to bc by pressing the key combination Ctrl+D.

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

echo '1+1' | bc
Using the shell's pipe, you can send more than For an operation, you need to use a semicolon to separate different operations. The results will be returned in separate lines.

echo '1+1; 2+2' | bc
precision

In some calculations, bc uses the concept of precision, the number of digits after the decimal point. The default precision is 0. The division operation always uses the precision setting. So, if you don't set the precision, you may get unexpected answers:

echo '3/2' | bc
echo 'scale=3; 3/2'
Multiplication uses a more sophisticated precision selection mechanism:

echo '3*2' | bc
echo '3*2.0' | bc
Meanwhile, the related operations for addition and subtraction are similar:

echo '7-4.15' | bc
Other bases Another useful feature of system

bc is that other counting systems than decimal can be used. For example, you can easily do hexadecimal or binary math. You can use the ibase and obase commands to set the base system for input and output, respectively. Keep in mind that once you use ibase, any numbers you enter after that will be considered to be in the newly defined base system.

To convert or operate a hexadecimal number to decimal, you can use a command like the following. Note that hexadecimal numbers greater than 9 must be in uppercase (AF):

echo 'ibase=16; A42F' | bc
echo 'ibase=16; 5F72+C39B' | bc
to make the result a hexadecimal number , you need to set obase:

echo 'obase=16; ibase=16; 5F72+C39B' | bc
Here is a little trick. If you do these hexadecimal operations in the shell, how do you get the input back to decimal? The answer is to use the ibase command, but you have to set it to the equivalent of 10 in decimal in the current base. For example, if ibase is set to hexadecimal, you need to enter:

ibase=A
Once you execute the above command, all entered numbers will be in decimal, then you can enter obase=10 to reset The base system of the output.

in conclusion

The above mentioned are just the basics of what bc can do. It also allows you to define functions, variables and looping structures for some complex operations and procedures. You can save these programs as text files on your system so you can use them when needed. You can also find more resources online that provide more examples and additional libraries. Happy counting!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326517468&siteId=291194637