博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux命令之bc - 浮点计算器、进制转换
阅读量:7114 次
发布时间:2019-06-28

本文共 4499 字,大约阅读时间需要 14 分钟。

hot3.png

用途说明

Bash内置了对整数四则运算的支持,但是并不支持浮点运算,而bc命令可以很方便的进行浮点运算,当然整数运算也不再话下。手册页上说bc是An arbitrary precision calculator language,即一个任意精度的计算语言,注意是一种语言,它提供了一些语法结构,比如条件判断、循环等,可以说是很强大的,但是我在实际中还没有找到需要这个用途的场合 。另外一个用途就是用来进行进制转换。

常用参数

一般情况下,我们使用不带任何参数的bc命令。

bc

如果需要bc不输出提示信息,可以加上-q参数:

bc -q

如果要使用强大的数学库,比如计算三角函数,需要加上-l参数:

bc -l

因为bc本身是一个命令解释器,要退出它只要直接输入quit回车或者按Ctrl+D终止。

使用示例

示例一 命令行方式使用bc

[root@localhost centos39]# bc

bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
3+4
7
3-4
-1
3*4
12
3/4
0
scale=2;3/4      # 保留小数点精度只对除法、取余、乘幂有效
.75
3/4
.75
3%4
0
scale=0
3%4
3
3^4
81

Ctrl+D

[root@localhost centos39]#

示例二 通过管道使用bc来计算

[root@localhost centos39]# echo 3 * 4 | bc

(standard_in) 1: parse error
[root@localhost centos39]# echo "3 * 4" | bc
12
[root@localhost centos39]# echo "scale=7; 355/113" | bc
3.1415929
[root@localhost centos39]#

Note:以上的例子我试过了,在我的机子里(windows xp sp3)是不能正常运行的,应该是吧echo后面的双引号无掉!!!

示例三 进制转换

[root@rhel55 ~]# echo "ibase=16; FFFF" | bc

65535

[root@rhel55 ~]# echo "obase=16; 1000" | bc

3E8
[root@rhel55 ~]#

示例四 将多个表达式写在一个文件中一起计算

[root@rhel55 ~]# cat test.bc

123*321
123/321
scale=4;123/321
[root@rhel55 ~]# bc test.bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
39483
0
.3831

Ctrl+D

[root@rhel55 ~]#
[root@rhel55 ~]# cat test.bc | bc
39483
0
.3831
[root@rhel55 ~]#

示例五 一个计算三角形面积的Bash脚本

先复习一下初中的知识:b表示三角形的底,h表示三角形的高,那么三角形的面积计算公式是b*h/2

文件 area_of_triangle.sh

Bash代码  
收藏代码
spinner.gif
  1. #!/bin/bash   
  2.   
  3. # Shell program/script to read the base and height of a traingle and find its area   
  4. # -------------------------------------------------------------------------   
  5. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>   
  6. # This script is licensed under GNU GPL version 2.0 or above   
  7. # -------------------------------------------------------------------------   
  8. # This script is part of nixCraft shell script collection (NSSC)   
  9. # Visit http://bash.cyberciti.biz/ for more information.   
  10. # -------------------------------------------------------------------------   
  11. # Formula info: http://www.mste.uiuc.edu/dildine/heron/triarea.html   
  12. # Area=(1/2) x Base x Height   
  13.   
  14. echo -n "Enter base of a triangle : "  
  15. read b   
  16.   
  17. echo -n "Enter height of a triangle : "  
  18. read h   
  19.   
  20. # calculate it and display back   
  21. area=$(echo "scale=2;(1/2) * $b * $h"|bc)   
  22. echo "Area of a triangle is $area"  
  23.    
#!/bin/bash# Shell program/script to read the base and height of a traingle and find its area# -------------------------------------------------------------------------# Copyright (c) 2005 nixCraft project 
# This script is licensed under GNU GPL version 2.0 or above# -------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC)# Visit http://bash.cyberciti.biz/ for more information.# -------------------------------------------------------------------------# Formula info: http://www.mste.uiuc.edu/dildine/heron/triarea.html# Area=(1/2) x Base x Heightecho -n "Enter base of a triangle : "read becho -n "Enter height of a triangle : "read h# calculate it and display backarea=$(echo "scale=2;(1/2) * $b * $h"|bc)echo "Area of a triangle is $area" 

 

[root@smsgw academic]# ./area_of_triangle.sh

Enter base of a triangle : 123
Enter height of a triangle : 321
Area of a triangle is 19741.50
[root@smsgw academic]#

 示例六 使用bc命令的脚本片段

Bash代码  
收藏代码
spinner.gif
  1. # usage: calc_sum <num1> <num2>   
  2. # 计算两个数的和   
  3. calc_sum()   
  4. {   
  5. bc -q <<EOF   
  6. $1+$2  
  7. EOF   
  8. }   
  9.   
  10. # usage: calc_free <count>   
  11. # 计算费用,单价0.05元   
  12. calc_fee()   
  13. {   
  14. bc -q <<EOF   
  15. 0.05*$1  
  16. EOF   
  17. }   
  18.     
# usage: calc_sum 
# 计算两个数的和calc_sum(){bc -q <
# 计算费用,单价0.05元calc_fee(){bc -q <

 

将以上代码粘贴到终端。

[root@web ~]# # usage: calc_sum <num1> <num2>

[root@web ~]# # 计算两个数的和
[root@web ~]# calc_sum()
> {
> bc -q <<EOF
> $1+$2
> EOF
> }
[root@web ~]#
[root@web ~]# # usage: calc_free <count>
[root@web ~]# # 计算费用,单价0.05元
[root@web ~]# calc_fee()
> {
> bc -q <<EOF
> 0.05*$1
> EOF
> }
[root@web ~]#
[root@web ~]#
[root@web ~]# calc_sum 123 321
444
[root@web ~]# calc_fee 1000
50.00
[root@web ~]#

示例七 使用数学库

有文章称可以计算100位的圆周率pi值。

[root@web ~]# echo "scale=100; a(1)*4" | bc

Runtime error (func=(main), adr=11): Function a not defined.
[root@web ~]# echo "scale=100; a(1)*4" | bc -l
3.141592653589793238462643383279502884197169399375105820974944592307\
8164062862089986280348253421170676
[root@web ~]#

转载于:https://my.oschina.net/wdliming/blog/37926

你可能感兴趣的文章
HDU 3689 KMP+DP
查看>>
算法实现c语言--03
查看>>
在web.xml不认<taglib>解决办法
查看>>
弹出窗口 刷新父页面 出现无法刷新网页 重试
查看>>
android BroadcastReceiver
查看>>
【下载】《美语语音训练》American Accent Training 文本+MP3
查看>>
UIAlertController高级之嵌入其他控件 分类: ios技术 ...
查看>>
[Java] JSP笔记 - EL、JSTL 常用标签
查看>>
git bash命令行
查看>>
【oracle】union、union all、intersect、minus 的用法及区别
查看>>
【转】交换机背板带宽
查看>>
tcpdump使用方法
查看>>
这样就可以修改MathType公式编号格式吗
查看>>
clean完后没有生成R文件
查看>>
一个男人关心的东西 决定了他的层次
查看>>
【转】七年阿里老人谈新人成长
查看>>
机器人搬重物(BFS)
查看>>
the config of vichrome
查看>>
VINS(七)estimator_node 数据对齐 imu预积分 vision
查看>>
pku1274 The Perfect Stall
查看>>