bc command

The bc command is a calculator language that supports arbitrary precision interactive execution. Bash has built-in support for four arithmetic operations on integers, but it does not support floating-point operations. The bc command can easily perform floating-point operations. Of course, integer operations are no longer a problem.

grammar

bc(options)(parameters)

Options

-i: Force into interactive mode;
-l: defines the standard math library used;
- w : warn about POSIX bc extensions;
-q: do not print normal GNU bc environment information;
-v: Display command version information;
-h: Display help information for the command.

parameter

File: Specify the file that contains the calculation task.

example

Arithmetic operations advanced operations bc command It can perform floating point operations and some advanced functions:

echo "1.212*3" | bc 
3.636

Set decimal precision (value range)

echo "scale=2;3/8" | bc
0.37

The parameter scale=2is to set the decimal place of the bc output result to 2.

base conversion

#!/bin/bash
abc=192
echo "obase=2;$abc" | bc

The execution result is: 11000000, which is to convert decimal to binary with bc.

#!/bin/bash
abc=11000000
echo "obase=10;ibase=2;$abc" | bc

The execution result is: 192, which is to convert binary to decimal with bc.

Compute squares and square roots:

echo "10^10" | bc
echo "sqrt(100)" | bc

Guess you like

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