Share a command-line calculator-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 called Calculator is already included in theFedora workstation . 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 tool called BC ? the following brothers to do an introduction to everyone:

BC Tools can provide you with the functionality 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 DNF 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:

$ BC

BC 1.06.95

Copyright 1991-1994, 1997, 1998, 2004, 2006 Free Software Foundation, Inc.

This is the free software with absolutely NO WARRANTY.

For details type ' warranty '.

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

Up

BC will answer the above formula:

2

You can also perform other commands here. You can use the plus (+), minus (-), multiply (*), Divide (/), parentheses, exponent symbols ( ^) 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) * *

4+7*2

To exit BC you can send an "input end" signal to BC by pressing 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 bring Echo the output is passed in BC Medium:

echo ' + ' | 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 ' +; The "| 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 use the ibase and obase commands to set the input and output system separately. It should be remembered that once you have used 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 the ibase command, but you must set it to a value equal to ten in the current binary . For example, if ibase is set to 16, you need to enter:

Ibase=a

Once you have executed the above command, all the numbers entered 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!

Share a command-line calculator-BC

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.