How is it an excellent command line calculator in Linux?

Source: Internet
Author: User
Tags echo command ibase mathematical functions natural logarithm

How is it an excellent command line calculator in Linux?

Each modern Linux Desktop release comes pre-installed with a graphical calculator program. However, if you have a command line window in your workspace, you will certainly handle some numeric problems in one of the command line windows. Maybe you are looking for a command line-based calculator program. In this case, GNU bc (short for "basic calculator") will be your best choice. Of course, there are many command line-based calculator applications in Linux. I think GNU bc is the most powerful and useful.

Before the GNU age, bc was actually a famous precision computing language. Its birth dates back to the Unix period of 1970s. Originally, bc was well known as a programming language with similar syntax and C language. With the change of time, the first bc evolved into POSIX bc, and finally became today's GNU bc.

Features of GNU bc

Now GNU bc is the result of several improvements and feature enhancements in early bc. Currently, it is stored by all mainstream GNU/Linux distributions. GNU bc supports high-precision numbers and input and output of multiple numeric types (such as binary, decimal, and hexadecimal.

If you are familiar with the C language, you will find that bc uses the same or similar arithmetic operators as the C language. Supported operators include Arithmetic Operators (+,-, *,/, %, ++, --), comparison operators (<,>, = ,! =, <=, >=), Logical operators (!, &, |), Bitwise operator (&, | ,,~, <,>) And composite value assignment operators (+ =,-=, * =,/=, % =, & =, | =, =, & =, | |=,<=, >>= ). Bc has many built-in useful functions, such as square root, sine, cosine, arcsin, natural logarithm, and exponent.

How to Use GNU bc

As a command line-based calculator, there is no limit on the use of GNU bc. In this article, I will introduce several common features of the bc command. For more detailed instructions, see the official guide.

If you do not have a pre-written bc script, you can run bc in interactive mode. In this mode, any declaration or expression you enter that ends with a carriage return will be calculated immediately. Enter the following command to enter the interaction interface of bc. To exit bc, enter 'quit' and press Enter.

  1. $ Bc

The following example is displayed in the bc interactive interface.

Input expression

To calculate an arithmetic expression, enter the expression at the blinking cursor and press enter to confirm. You can also store the result in a variable and use the variable in other expressions.

In a bc interaction interface, there is no limit on the number of command history records. Use the up arrow key to view the previously entered command. If you want to limit the number of commands saved in the history, you can set a special variable named history to the value you want. The default value of this variable is-1, indicating that there is no limit on the number of historical records ".

Input/Output hexadecimal Switch

It often happens that you enter an expression and want to use binary or hexadecimal to display the result. Bc allows you to convert Input and Output numbers in the hexadecimal format. The numbers of input and output are stored in the ibase and obase variables. The default value is 10, and the valid value is 2 to 16 (or the environment variable BC ).BASEMAX value ). You only need to change the values of ibase and obase to convert the values between different hexadecimal formats. The following is an example of how to calculate the sum of two hexadecimal/binary numbers:

It should be noted that I intentionally put obase = 16 before ibase = 16, which in turn cannot. This is because if I enter ibase = 16 first, then the 16 in the input obase = 16 will be considered as a hexadecimal number, that is, 22 in decimal format. Of course, this is not what we expect.

Precision Adjustment

In bc, the precision of numbers is stored in a special variable named scale. This variable indicates the number of digits after the decimal point. The default value of scale is 0, which means that all numbers and results are stored as integers. You can adjust the precision of the value by changing the value of the scale special variable.

  1. Scale = 4

Use built-in functions

In addition to simple arithmetic operators, GNU bc provides many advanced mathematical functions through external mathematical libraries. You can use the "-l" option on the command line interface to open bc.

Some built-in functions are described here.

The second root of N:

  1. Sqrt (N)

Sine of X (X is a radian ):

  1. S (X)

Cosine of X (X is a radian ):

  1. C (X)

Returns the arc sine of X (radians ):

  1. A (X)

Natural logarithm of X:

  1. L (X)

Exponential logarithm of X:

  1. E (X)
Used as a language

As a computing language, GNU bc supports Simple declarations (variable assignment, interrupt, return, etc.), composite statements (if, while, for loop, etc.), and user-defined functions. I won't discuss the details of these features here, but you can use the official guide to learn how to use these features. The following is a simple function example:

  1. Define dummy (x ){
  2. Return (x * x );
  3. }
  4. Dummy (9)
  5. 81
  6. Dummy (4)
  7. 16
Use GNU bc on non-interactive interfaces

So far, we have been using bc in the interactive interface. However, the more popular method of using bc is to run bc in a script without an interactive interface. In this case, you can use the echo command and use the pipeline to send input content to bc. For example:

  1. $ Echo "40*5" | bc
  2. $ Echo "scale = 4; 10/3" | bc
  3. $ Echo "obase = 16; ibase = 2, 11101101101100010" | bc

To sum up, GNU bc is a powerful and common command line-based calculator application that will never disappoint you. It is pre-installed in all modern Linux releases, and bc allows you to perform efficient mathematical computing without having to leave the command line. Therefore, GNU bc will be your favorite.

This article permanently updates the link address:

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.