A command-line calculator in linux introduction to BC

Source: Internet
Author: User
Tags ibase

If you need a calculator in a graphical desktop environment, you may only need to click on it all the way to find a calculator. For example, a tool named is already included in the Fedora workstation Calculator . It has several different modes of operation, for example, you can perform complex mathematical operations or financial operations. But, you know, the command line also provides a similar named bc tool?

bcTools can give you the ability to meet your expectations for scientific calculators, financial calculators, or simple calculators. Alternatively, it can be scripted from the command line, if needed. This allows you to use it in a shell script when you need to do a complex math operation.

Because BC is also used for other system software, such as the CUPS Print Service, it may already be installed in your Fedora system. You can use the following command to check:

DNF List installed BC

If for some reason you do not see it in the output of the above command, you can use this command to install it:

sudo Install BC
Use BC to do some simple math operations

One way to use BC is to enter its own shell. There you can do many calculations by line. When you type BC, the first thing that comes up is a warning about the program:

1.06.  the  1991-1994199719982004 2006    free software with absolutely NO WARRANTY. For details type ' warranty'.

Now you can follow an input expression or command for each line:

1+1

BC will answer the above formula:

2

You can also perform other commands here. You can use Add (+), subtract (-), multiply (*), divide (/), parentheses, exponent (^), and so on. Please note that BC also follows all the established rules of operation, such as the sequencing of operations. You can try the following example:

(4+7) *24+7*2

To exit BC You can send an Ctrl+D "input end" signal to BC by pressing the key combination.

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

Echo '  up ' | Bc

Using the Shell's pipeline, you can send more than one operation, you need to use semicolons to separate the different operations. The result is returned in a different row.

Echo ' A . ' | Bc

 

Precision

In some calculations, BC uses the concept of precision, which is the number of digits after the decimal point. The default precision is 0. The division operation always uses the precision setting. Therefore, if you do not set the accuracy, it may bring unexpected answers:

Echo ' 3/2 ' | BC Echo ' scale=3; 3/2 ' | Bc

Multiplication uses a more complex precision-selection mechanism:

Echo ' 3*2 ' | BC Echo ' 3*2.0 ' | Bc

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

Echo ' 7-4.15 ' | Bc
Other input systems

Another useful feature of BC is that it is possible to use a counting system other than decimal. For example, you can easily do a hexadecimal or binary mathematical operation. You can ibase use obase the and commands to set the input and output of the binary system separately. It is necessary to remember that once you have used it ibase , any number you enter will be considered to be in the newly defined binary system.

To do a hexadecimal number to decimal number conversion or operation, you can use a command similar to the following. Note that the hexadecimal number greater than 9 must be uppercase (A-F):

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's a little trick. If you do these hexadecimal operations in the shell, how can you make the input a decimal number? The answer is to use a ibase command, but you must set it to a value equivalent to 10 in the decimal in the current binary. For example, if it ibase is set to 16, you need to enter:

Ibase=a

Once you have executed the above command, all the input numbers will be in decimal, and then you can enter obase=10 to reset the output of the binary system.

Conclusion

The above mentioned is only the basis that BC can do. It also allows you to define functions, variables, and loop structures for some complex operations and programs. You can save these programs as text files in your system so that you use them when you need them. You can also find more resources on the web, which provide more examples and additional library of functions. Calculate it with pleasure!

A command-line calculator in linux introduction to BC

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.