Linux计算器bc

Linux计算器bc

功能强大

  • 基本计算器功能
  • 支持变量、函数,条件和循环 等编程 功能(类似 C 语言的小编程语言)
  • 可以进行任意精度的 计算

精度

命令设置缺省精度

打开计算器的命令 描述
bc 缺省精度为小数点后 0 位
bc l 缺省精度 为小数点后 20 位

整数计算

1
2
3
4
5
6
7
8
[root@localhost ~]# 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/4
2

小数计算

1
2
3
4
5
6
7
8
[root@localhost ~]# bc -l
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/4
2.50000000000000000000

可以通过设置scale自行决定精度(小数点位数)

设置bc计算器精确到小数点后10000位

1
scale=10000

计算效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# 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/4
2
scale=10000
1+2*3/4
2.500000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\

设置bc计算器精确到小数点后30位

1
2
3
4
scale=30 #输入
1+2*3/4 #输入
2.500000000000000000000000000000 #计算结果

bc编程

使用变量

查看变量

在bc中输入变量的值,然后在输入变量名即可查看该变量的值:

1
2
p=20
p

运行效果

1
2
3
p=20
p
20

使用变量进行计算

1
2
3
4
5
6
7
8
9
[root@localhost ~]# 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'.
p=20 #输入
(2+p)*p #输入
440 #计算结果

使用循环

求1~128的阶乘,在bc中分别输入如下三行代码:

1
2
3
s=1;          
for(i=1;i<128;i++) s*=i;
s

计算结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# 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'.
s=1; #输入
for(i=1;i<128;i++) s*=i; #输入
s #输入
30126600184576595448099770775270596923241649186736217990533469005966\
67207618480809067860692097713761984609779945772783965563851033300772\
32629777308785186998250027066179124412259762176000000000000000000000\
0000000000