Implementation of mathematical calculator with Linux BC command


Bash shell has built-in support for integer operation, but does not support floating-point operation. Linux BC command can easily carry out floating-point operation. Of course, integer operation is no longer required.

BC can even be called a programming language. It supports basic programming elements such as variables, arrays, input and output, branch structure, loop structure and functions. Therefore, BC is described in the Linux manual as follows:

An arbitrary precision calculator language

It translates into “a calculator language with arbitrary precision”.

Input at terminalbcCommand, and then press enter to enter BC for interactive mathematical calculation. In shell programming, we can also use BC through pipeline and input redirection.

In this section, we first learn how to use BC in an interactive environment, and then learn how to use BC in shell programming.

Enter BC from terminal

Enter the BC command at the terminal and press enter to enter BC. See the figure below:

The BC command also has some options that you may use. Please see the table below.

option explain
-h | –help Help information
-v | –version Display command version information
-l | –mathlib Using the standard math library
-i | –interactive Force interaction
-w | –warn Displays a warning message for POSIX
-s | –standard Use POSIX standard to handle
-q | –quiet Do not display welcome messages

For example, if you don’t want to display a pile of useless information after entering the BC command, you can enterbc -q

Using BC in an interactive environment

Using BC for mathematical calculation is very easy. Enter the mathematical expression as usual, and then press enter to see the result. Please see the figure below.

It is worth mentioning that we define a variable n, and then use n in the calculation. It can be seen that BC supports variables.

In addition to variables, BC also supports common programming elements such as functions, loop structures and branch structures, which are similar to the syntax of other programming languages. Next, we define a factorial function:

In fact, we rarely use such complex functions. In most cases, we still use BC as an ordinary mathematical calculator to find the value of the expression, so we don’t have to study it deeply.

Built in variable

BC has four built-in variables, which we often use in calculation, as shown in the following table:

Variable name effect
scale Specify the precision, that is, the number of digits after the decimal point; The default is 0, that is, the decimal part is not used.
ibase Specifies the base of the entered number. The default is decimal.
obase Specifies the base of the output number. The default is decimal.
Last or Represents the most recently printed number

[example 1] example of scale variable usage:

At the beginning, the value of 10 / 3 is 3 without decimal part, because the default value of scale variable is 0; After you specify a value greater than 0 for scale, you can see the decimal part.

[example 2] usage examples of IBASE and obase variables:

Note: obase should be placed in front of IBASE as much as possible, because after IBASE is set, the following numbers are converted by IBASE base.

Built in function

In addition to built-in variables, BC also has some built-in functions, as shown in the following table:

Function name effect
s(x) Calculate the sine of X, which is the radian value.
c(x) Calculates the cosine of X, which is the radian value.
a(x) Calculates the arctangent of X and returns the radian value.
l(x) Calculate the natural logarithm of X.
e(x) Find the x power of E.
j(n, x) Bessel function, calculate the order from n to X.

To use these mathematical functions, you need to use when entering the BC command-lOption to enable the math library. Take the following example:

Use multiple expressions in a row

In the previous example, we basically have one expression per line, which makes it more comfortable; You can also put multiple expressions on one line if you like, just use semicolons;Just separate it. Take the following example:

Using BC calculator in shell

In shell script, we can use BC calculator by means of pipeline or input redirection.

  • Pipeline is a communication mechanism between Linux processes. It can take the output of the previous command (process) as the input of the next command (process), and a vertical bar is used between the two commands|separate.
  • Usually, a command gets the user’s input from the terminal. If it gets input from other places (such as files), it needs to be redirected.

We do not intend to explain the pipeline and redirection here. Please Baidu yourself if you don’t understand.

Using BC calculator with pipe

If the reader wants to directly output the calculation result of BC, the following form can be used:

echo "expression" | bc

expressionIt is the mathematical expression that you want to calculate. It must comply with BC syntax, which we have introduced above. In expression, you can also use variables in shell scripts.

The calculation result of BC can be assigned to the shell variable in the following form:

variable=$(echo "expression" | bc)

Variable is the name of the variable.

[example 1] the simplest form:

[]$ echo "3*8"|bc
[]$ ret=$(echo "4+9"|bc)
[]$ echo $ret

[example 2] use the variables in BC:

[]$ echo "scale=4;3*8/7"|bc
[]$ echo "scale=4;3*8/7;last*5"|bc

[example 3] using variables in shell script:

[]$ x=4
[]$ echo "scale=5;n=$x+2;e(n)"|bc -l

In the second command, $X means to use the variable defined in the first shell command, and N is the new variable defined in BC, which has nothing to do with the shell script.

[example 4] binary conversion:

#Decimal to hexadecimal
[[email protected] ~]$ m=31
[[email protected] ~]$ n=$(echo "obase=16;$m"|bc)
[[email protected] ~]$ echo $n
#Hex to decimal
[[email protected] ~]$ m=1E
[[email protected] ~]$ n=$(echo "obase=10;ibase=16;$m"|bc)
[[email protected] ~]$ echo $n

Using the BC calculator with input redirection

The calculation result of BC can be assigned to the shell variable in the following form:

variable=$(bc << EOF

Among them, variable is the shell variable name, express is the mathematical expression to be calculated (it can wrap lines, which is the same as the writing form after entering BC), and EOF is the start and end identification of the mathematical expression (you can also change it to other names, such as AAA, BBB, etc.).

Take the following example:

[]$ m=1E
[]$ n=$(bc << EOF
> obase=10;
> ibase=16;
> print $m
> )
[]$ echo $n

If you have a lot of mathematical calculations, it is more convenient to use input redirection, because mathematical expressions can wrap and write more clearly.

This is the end of this article about the implementation of Linux BC command math calculator. For more information about Linux BC math calculator, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

Crash tracking journey of IOS development (I)

Preface: Recently, I encountered a crash blood disaster caused by crash in daily development. In a release in early May, the crash rate of the app developed by the author was directly increased from one thousand to nearly two thousand. At that time, the project leader just needed to report the relevant situation of project […]