bc 用于基本计算器(basic calculator)(通常称为台式计算器(bench calculator)),是“一种任意精度的计算器语言”,其语法类似于 C 编程语言。bc 通常用作数学脚本语言或交互式数学 shell.
安装
1 2 3 4 5 6 7 8
# Debian/Ubuntu sudo apt install bc
# RHEL/CentOS sudo yum install bc
# Fedora 22+ sudo dnf install bc
基本用法
使用下面命令查看 bc 基本用法:
1
man bc
使用下面命令查看 bc 常用帮助:
1
bc -h
结果如下:
1 2 3 4 5 6 7 8
usage: bc [options] [file ...] -h --help print this usage and exit -i --interactive force interactive mode -l --mathlib use the predefined math routines -q --quiet don't print initial banner -s --standard non-standard bc constructs are errors -w --warn warn about non-standard bc constructs -v --version print version information and exit
基本使用: 直接在终端输入 bc,即可以交互式方式进行数学计算:
1
bc
退出交换模式,请输入:
1
quit
打开交换终端时导入数学模块(mathlib),方法如下:
1
bc -l
使用此方法可以调用常用数学方法,如计算圆周率:
1 2 3 4 5 6 7
$ bc -l bc 1.07.1 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 4 * a(1) 3.14159265358979323844
# 注意,默认输入输出都是十进制 $ echo"obase=10;ibase=2;11" | bc 3 # 此时 10 已经被 ibase=2 认为时二进制的 10,即输出也是二进制 $ echo"ibase=2;obase=10;11" | bc 11
除了 0-9 和 A-F 外,其他字母 G-Z 也有默认值,分别依次累加。如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
$ bc -q A 10 f # 必须大写 0 F 15 G # 延续 F 依次累加 1 16 H 17 X 33 Y 34 Z # 直到 Z 35
表达式
基本表达式
- expr 取相反数
++ var 变量 var 累加 1,返回结果是 var
-- var 变量 var 减少 1,返回结果是 var
var ++ 变量 var 累加 1,返回结果是累加 1 前的 var
var -- 变量 var 减少 1,返回结果是减少 1 前的 var
expr + expr 两项求和
expr - expr 两项求差
expr * expr 两项相乘
expr / expr 两项相除
expr % expr 两项求余数 The result of the expression is the “remainder” and it is computed in the following way. To compute a%b, first a/b is computed to scale digits. That result is used to compute a-(a/b)*b to the scale of the maximum of scale+scale(b) and scale(a). If scale is set to zero and both expressions are integers this expression is the integer remainder function.
expr ^ expr 两项求幂 The result of the expression is the value of the first raised to the second. The second expression must be an integer. (If the second expression is not an integer, a warning is generated and the expression is truncated to get an integer value.) The scale of the result is scale if the exponent is negative. If the exponent is positive the scale of the result is the minimum of the scale of the first expression times the value of the exponent and the maximum of scale and the scale of the first expression. (e.g. scale(a^b) = min(scale(a)*b, max(scale, scale(a))).) It should be noted that expr^0 will always return the value of 1.
( expr ) 改变运算顺序
var = expr 变量赋值
var <op>= expr This is equivalent to “var = var expr” with the exception that the “var” part is evaluated only once. This can make a difference if “var” is an array.
关系表达式
expr1 < expr2 The result is 1 if expr1 is strictly less than expr2.
expr1 <= expr2 The result is 1 if expr1 is less than or equal to expr2.
expr1 > expr2 The result is 1 if expr1 is strictly greater than expr2.
expr1 >= expr2 The result is 1 if expr1 is greater than or equal to expr2.
expr1 == expr2 The result is 1 if expr1 is equal to expr2.
expr1 != expr2 The result is 1 if expr1 is not equal to expr2.
布尔表达式
!expr The result is 1 if expr is 0.
expr && expr The result is 1 if both expressions are non-zero.
expr || expr The result is 1 if either expression is non-zero.
表达式优先级
从上往下优先级递增
1 2 3 4 5 6 7 8 9 10
|| && ! 关系表达式 赋值表达式 +, - *, /, % ^ - ++, --
特殊表达式
length(var) 获取有效位长度
read() 读取值
scale(var) 获取小数点后有效位长度
sqrt(expr) 求平方根
数学库
bc -l 能够调用数学库,且保留精度默认设置为 scale=20,常见数学公式如下:
s(x) The sine of x, x is in radians.
c(x) The cosine of x, x is in radians.
a(x) The arctangent of x, arctangent returns radians.
l(x) The natural logarithm of x.
e(x) The exponential function of raising e to the value x.
j(n, x) The bessel function of integer order n of x.
复杂表达式
print list The print statement (an extension) provides another method of output. The list is a list of strings and expressions separated by commas. Each string or expression is printed in the order of the list. No terminating newline is printed. Expressions are evaluated and their value is printed and assigned to the variable last. Strings in the print statement are printed to the output and may contain special characters. Special characters start with the backslash character (\e). The special characters recognized by bc are “a” (alert or bell), “b” (backspace), “f” (form feed), “n” (newline), “r” (carriage return), “q” (double quote), “t” (tab), and “\e” (backslash). Any other character following the backslash will be ignored.
{ statement_list } This is the compound statement. It allows multiple statements to be grouped together for execution.
if ( expression ) statement1 [else statement2] The if statement evaluates the expression and executes statement1 or statement2 depending on the value of the expression. If the expression is non-zero, statement1 is executed. If statement2 is present and the value of the expression is 0, then statement2 is executed. (The else clause is an extension.)
while ( expression ) statement The while statement will execute the statement while the expression is non-zero. It evaluates the expression before each execution of the statement. Termination of the loop is caused by a zero expression value or the execution of a break statement.
for ( [expression1] ; [expression2] ; [expression3] ) statement The for statement controls repeated execution of the statement. Expression1 is evaluated before the loop. Expression2 is evaluated before each execution of the statement. If it is non-zero, the statement is evaluated. If it is zero, the loop is terminated. After each execution of the statement, expression3 is evaluated before the reevaluation of expression2. If expression1 or expression3 are missing, nothing is evaluated at the point they would be evaluated. If expression2 is missing, it is the same as substituting the value 1 for expression2. (The optional expressions are an extension. POSIX bc requires all three expressions.) The following is equivalent code for the for statement:
1 2 3 4 5
expression1; while (expression2) { statement; expression3; }
break This statement causes a forced exit of the most recent enclosing while statement or for statement. continue The continue statement (an extension) causes the most recent enclosing for statement to start the next iteration.
halt The halt statement (an extension) is an executed statement that causes the bc processor to quit only when it is executed. For example, “if (0 == 1) halt” will not cause bc to terminate because the halt is not executed.
return Return the value 0 from a function. (See section Functions.)
return ( expression ) Return the value of the expression from a function. (See section Functions.) As an extension, the parenthesis are not required.
自定义函数
1 2
define name ( parameters ) { newline auto_list statement_list }