Linux easy to use calculator --bc command (command calculator)

bc arbitrary precision calculator language command is usually used as a calculator at linux. It is similar to the basic calculator, use this calculator can do basic math.

Note: Enter quit to exit the command.

grammar:

bc(选项)(参数)

Option Value

  • -i: forced into interactive mode;
  • -l: the standard math library definitions used
  • ; -W: extended POSIX bc given a warning;
  • -q: Do not print the normal GNU bc environmental information;
  • -v: show version command information;
  • -h: displays help information instruction.

parameter

File: Specifies the file that contains the computing tasks.

Common operations:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • ^ Index
  • % remainder

Note: The default calculation conforming to different mixing stages when you calculate parentheses calculated (/ need extra attention or%), such as: 2 + 2 + 5 = 10 * (5 * 10) = 52 

E.g

1, the conventional computing

[hern@hern ~]$ echo "1+2+3" |bc
6
[hern@hern ~]$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1+2+3        #<==只有加法时
6            #<==输出结果
7-8+3        #<==加法与减法混合时
2
10*5+2       #<==乘法与加法混合
52
10%3         #<==余数计算
1
10^2         #<==平方计算
100
10/100       #<==除数取整
0
2+5*10       #<==加法与乘法混合时
52

2, several setting (scale by setting) after the decimal point is output:

[hern@hern ~]$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
scale=3            #<==设置输出小数点后3位
1/3
.333               #<==输出小数点后3位
340/2349
.144
quit
[hern@hern ~]$ echo "scale=2;(2.77-1.4744)/1" |bc
1.29

3, ibase to hexadecimal arithmetic and obase

[hern@hern ~]$ echo "ibase=2;111" |bc        #<==输出二进制111的结果
7

 

Published 705 original articles · won praise 666 · Views 1.43 million +

Guess you like

Origin blog.csdn.net/qq_36761831/article/details/104775350