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

注意:导入数学模块后默认保留小数点 20 位;不导入数学模块时默认保留小数点 0 位。见如下示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 不导入数学模块
$ echo "3/2" | bc
1
# 导入数学模块
$ echo "3/2" | bc -l
1.50000000000000000000
# 只有导入数学模块采用调用反正切(arctangent)函数:a(x)
# scale 变量表示设置精度(小数点后有效位数)为 10000,默认是 20
$ echo "scale=10000;4*a(1)" | bc -l
3.141592653589793238462643383279502884197169399375105820974944592307\
81640628620899862803482534211706798214808651328230664709384460955058\
22317253594081284811174502841027019385211055596446229489549303819644\
28810975665933446128475648233786783165271201909145648566923460348610\
45432664821339360726024914127372458700660631558817488152092096282925\
40917153643678925903600113305305488204665213841469519415116094330572\
70365759591953092186117381932611793105118548074462379962749567351885\
75272489122793818301194912983367336244065664308602139494639522473719\
07021798609437027705392171762931767523846748184676694051320005681271\
45263560827785771342757789609173637178721468440901224953430146549585\
37105079227968925892354201995611212902196086403441815981362977477130\
99605187072113499999983729780499510597317328160963185950244594553469\
08302642522308253344685035261931188171010003137838752886587533208381\
42061717766914730359825349042875546873115956286388235378759375195778\
18577805321712268066130019278766111959092164201989380952572010654858\
63278865936153381827968230301952035301852968995773622599413891249721\
77528347913151557485724245415069595082953311686172785588907509838175\
46374649393192550604009277016711390098488240128583616035637076601047\
10181942955596198946767837449448255379774726847104047534646208046684\
25906949129331367702898915210475216205696602405803815019351125338243\
00355876402474964732639141992726042699227967823547816360093417216412\
19924586315030286182974555706749838505494588586926995690927210797509\
30295532116534498720275596023648066549911988183479775356636980742654\
25278625518184175746728909777727938000816470600161452491921732172147\
72350141441973568548161361157352552133475741849468438523323907394143\
33454776241686251898356948556209921922218427255025425688767179049460\
16534668049886272327917860857843838279679766814541009538837863609506\
80064225125205117392984896084128488626945604241965285022210661186306\
74427862203919494504712371378696095636437191728746776465757396241389\
08658326459958133904780275900994657640789512694683983525957098258226\
20522489407726719478268482601476990902640136394437455305068203496252\
45174939965143142980919065925093722169646151570985838741059788595977\
29754989301617539284681382686838689427741559918559252459539594310499\
72524680845987273644695848653836736222626099124608051243884390451244\
13654976278079771569143599770012961608944169486855584840635342207222\
58284886481584560285060168427394522674676788952521385225499546667278\
23986456596116354886230577456498035593634568174324112515076069479451\
09659609402522887971089314566913686722874894056010150330861792868092\
08747609178249385890097149096759852613655497818931297848216829989487\
22658804857564014270477555132379641451523746234364542858444795265867\
82105114135473573952311342716610213596953623144295248493718711014576\
54035902799344037420073105785390621983874478084784896833214457138687\
51943506430218453191048481005370614680674919278191197939952061419663\
42875444064374512371819217999839101591956181467514269123974894090718\
64942319615679452080951465502252316038819301420937621378559566389377\
87083039069792077346722182562599661501421503068038447734549202605414\
66592520149744285073251866600213243408819071048633173464965145390579\
62685610055081066587969981635747363840525714591028970641401109712062\
80439039759515677157700420337869936007230558763176359421873125147120\
53292819182618612586732157919841484882916447060957527069572209175671\
16722910981690915280173506712748583222871835209353965725121083579151\
36988209144421006751033467110314126711136990865851639831501970165151\
16851714376576183515565088490998985998238734552833163550764791853589\
32261854896321329330898570642046752590709154814165498594616371802709\
81994309924488957571282890592323326097299712084433573265489382391193\
25974636673058360414281388303203824903758985243744170291327656180937\
73444030707469211201913020330380197621101100449293215160842444859637\
66983895228684783123552658213144957685726243344189303968642624341077\
32269780280731891544110104468232527162010526522721116603966655730925\
47110557853763466820653109896526918620564769312570586356620185581007\
29360659876486117910453348850346113657686753249441668039626579787718\
55608455296541266540853061434443185867697514566140680070023787765913\
44017127494704205622305389945613140711270004078547332699390814546646\
45880797270826683063432858785698305235808933065757406795457163775254\
20211495576158140025012622859413021647155097925923099079654737612551\
76567513575178296664547791745011299614890304639947132962107340437518\
95735961458901938971311179042978285647503203198691514028708085990480\
10941214722131794764777262241425485454033215718530614228813758504306\
33217518297986622371721591607716692547487389866549494501146540628433\
66393790039769265672146385306736096571209180763832716641627488880078\
69256029022847210403172118608204190004229661711963779213375751149595\
01566049631862947265473642523081770367515906735023507283540567040386\
74351362222477158915049530984448933309634087807693259939780541934144\
73774418426312986080998886874132604721569516239658645730216315981931\
95167353812974167729478672422924654366800980676928238280689964004824\
35403701416314965897940924323789690706977942236250822168895738379862\
30015937764716512289357860158816175578297352334460428151262720373431\
46531977774160319906655418763979293344195215413418994854447345673831\
62499341913181480927777103863877343177207545654532207770921201905166\
09628049092636019759882816133231666365286193266863360627356763035447\
76280350450777235547105859548702790814356240145171806246436267945612\
75318134078330336254232783944975382437205835311477119926063813346776\
87969597030983391307710987040859133746414428227726346594704745878477\
87201927715280731767907707157213444730605700733492436931138350493163\
12840425121925651798069411352801314701304781643788518529092854520116\
58393419656213491434159562586586557055269049652098580338507224264829\
39728584783163057777560688876446248246857926039535277348030480290058\
76075825104747091643961362676044925627420420832085661190625454337213\
15359584506877246029016187667952406163425225771954291629919306455377\
99140373404328752628889639958794757291746426357455254079091451357111\
36941091193932519107602082520261879853188770584297259167781314969900\
90192116971737278476847268608490033770242429165130050051683233643503\
89517029893922334517220138128069650117844087451960121228599371623130\
17114448464090389064495444006198690754851602632750529834918740786680\
88183385102283345085048608250393021332197155184306354550076682829493\
04137765527939751754613953984683393638304746119966538581538420568533\
86218672523340283087112328278921250771262946322956398989893582116745\
62701021835646220134967151881909730381198004973407239610368540664319\
39509790190699639552453005450580685501956730229219139339185680344903\
98205955100226353536192041994745538593810234395544959778377902374216\
17271117236434354394782218185286240851400666044332588856986705431547\
06965747458550332323342107301545940516553790686627333799585115625784\
32298827372319898757141595781119635833005940873068121602876496286744\
60477464915995054973742562690104903778198683593814657412680492564879\
85561453723478673303904688383436346553794986419270563872931748723320\
83760112302991136793862708943879936201629515413371424892830722012690\
14754668476535761647737946752004907571555278196536213239264061601363\
58155907422020203187277605277219005561484255518792530343513984425322\
34157623361064250639049750086562710953591946589751413103482276930624\
74353632569160781547818115284366795706110861533150445212747392454494\
54236828860613408414863776700961207151249140430272538607648236341433\
46235189757664521641376796903149501910857598442391986291642193994907\
23623464684411739403265918404437805133389452574239950829659122850855\
58215725031071257012668302402929525220118726767562204154205161841634\
84756516999811614101002996078386909291603028840026910414079288621507\
84245167090870006992821206604183718065355672525325675328612910424877\
61825829765157959847035622262934860034158722980534989650226291748788\
20273420922224533985626476691490556284250391275771028402799806636582\
54889264880254566101729670266407655904290994568150652653053718294127\
03369313785178609040708667114965583434347693385781711386455873678123\
01458768712660348913909562009939361031029161615288138437909904231747\
33639480457593149314052976347574811935670911013775172100803155902485\
30906692037671922033229094334676851422144773793937517034436619910403\
37511173547191855046449026365512816228824462575916333039107225383742\
18214088350865739177150968288747826569959957449066175834413752239709\
68340800535598491754173818839994469748676265516582765848358845314277\
56879002909517028352971634456212964043523117600665101241200659755851\
27617858382920419748442360800719304576189323492292796501987518721272\
67507981255470958904556357921221033346697499235630254947802490114195\
21238281530911407907386025152274299581807247162591668545133312394804\
94707911915326734302824418604142636395480004480026704962482017928964\
76697583183271314251702969234889627668440323260927524960357996469256\
50493681836090032380929345958897069536534940603402166544375589004563\
28822505452556405644824651518754711962184439658253375438856909411303\
15095261793780029741207665147939425902989695946995565761218656196733\
78623625612521632086286922210327488921865436480229678070576561514463\
20469279068212073883778142335628236089632080682224680122482611771858\
96381409183903673672220888321513755600372798394004152970028783076670\
94447456013455641725437090697939612257142989467154357846878861444581\
23145935719849225284716050492212424701412147805734551050080190869960\
33027634787081081754501193071412233908663938339529425786905076431006\
38351983438934159613185434754649556978103829309716465143840700707360\
41123735998434522516105070270562352660127648483084076118301305279320\
54274628654036036745328651057065874882256981579367897669742205750596\
83440869735020141020672358502007245225632651341055924019027421624843\
91403599895353945909440704691209140938700126456001623742880210927645\
79310657922955249887275846101264836999892256959688159205600101655256\
375676

bc 支持以下功能:

  1. 算术运算;
  2. 递增或递减运算;
  3. 赋值运算;
  4. 比较或关系运算;
  5. 逻辑或布尔运算;
  6. 数学函数;
  7. 条件语句;
  8. 迭代语句。

数字

bc 中最基本的元素是数字,任意精度的数字。所有数字都以十进制表示和计算。每一个数字都有两个属性,分别是 lengthscalelength 表示数字中所有字母的个数(包括整数位、小数位,但不包括小数点),scale 只表示小数位的个数。示例:

1
2
3
4
5
6
$ bc -q
a = 3.21
length(a)
3
scale(a)
2

注释

bc 作为一门编程语言,必定会有注释要求。单号注释使用 #,多行注释使用 /**/,示例如下:
未赋值的变量使用时,默认赋值为 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ bc -q
# a = 3
a
0
b = 3
b
3
/* a = 2
b = 2
*/
a
0
b
3

进制转换

bc 作为 Linux 系统的计算器,支持进度转换,转换范围从 2 到 16 都支持。ibase表示输入数字的进制,obase表示输出数字的进制。输入数字包含字符 0-9 和 A-F,必须大写,小写表示变量符号。默认 ibase=10;obase=10,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ bc -q
11
11
ibase=2
11
3
ibase=10000 # 注意因为上面已经设置成2进制输入,所以 16 必须用二进制 10000 表示
11
17
ibase=A;obase=8 # 此时输入是16进制,设置为十进制用 A 表示
9
11
ibase=8;obase=9
11
10

特别注意:设置进制时,之前的设置会影响当前的设置,当一次设置调用时,建议先设置 obase,示例如下:

1
2
3
4
5
6
# 注意,默认输入输出都是十进制
$ 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

表达式

基本表达式

  1. - expr 取相反数
  2. ++ var 变量 var 累加 1,返回结果是 var
  3. -- var 变量 var 减少 1,返回结果是 var
  4. var ++ 变量 var 累加 1,返回结果是累加 1 前的 var
  5. var -- 变量 var 减少 1,返回结果是减少 1 前的 var
  6. expr + expr 两项求和
  7. expr - expr 两项求差
  8. expr * expr 两项相乘
  9. expr / expr 两项相除
  10. 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.
  11. 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.
  12. ( expr ) 改变运算顺序
  13. var = expr 变量赋值
  14. 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.

关系表达式

  1. expr1 < expr2 The result is 1 if expr1 is strictly less than expr2.
  2. expr1 <= expr2 The result is 1 if expr1 is less than or equal to expr2.
  3. expr1 > expr2 The result is 1 if expr1 is strictly greater than expr2.
  4. expr1 >= expr2 The result is 1 if expr1 is greater than or equal to expr2.
  5. expr1 == expr2 The result is 1 if expr1 is equal to expr2.
  6. expr1 != expr2 The result is 1 if expr1 is not equal to expr2.

布尔表达式

  1. !expr The result is 1 if expr is 0.
  2. expr && expr The result is 1 if both expressions are non-zero.
  3. expr || expr The result is 1 if either expression is non-zero.

表达式优先级

从上往下优先级递增

1
2
3
4
5
6
7
8
9
10
||
&&
!
关系表达式
赋值表达式
+, -
*, /, %
^
-
++, --

特殊表达式

  1. length(var) 获取有效位长度
  2. read() 读取值
  3. scale(var) 获取小数点后有效位长度
  4. sqrt(expr) 求平方根

数学库

bc -l 能够调用数学库,且保留精度默认设置为 scale=20,常见数学公式如下:

  1. s(x)
    The sine of x, x is in radians.
  2. c(x)
    The cosine of x, x is in radians.
  3. a(x)
    The arctangent of x, arctangent returns radians.
  4. l(x)
    The natural logarithm of x.
  5. e(x)
    The exponential function of raising e to the value x.
  6. j(n, x)
    The bessel function of integer order n of x.

复杂表达式

  1. 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.
  2. { statement_list }
    This is the compound statement. It allows multiple statements to be grouped together for execution.
  3. 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.)
  4. 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.
  5. 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;
    }
  6. 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.
  7. 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.
  8. return
    Return the value 0 from a function. (See section Functions.)
  9. 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 }

示例:

1
2
3
4
define d(n) { return (2*n); }
# 或者
define d(n)
{ return (2*n); }
1
2
3
4
5
6
$ bc -q
define m2(n) {return (2*n);}
m2(3)
6
m2(9)
18

参考文献

  1. an arbitrary precision calculator language
  2. bc command in Linux with examples
  3. Shell中计算模块 bc的使用
  4. Linux下使用bc进行计算公式