Linux命令之bc - 浮點計算器、進制轉換

用途說明

Bash內置了對整數四則運算的支持,可是並不支持浮點運算,而bc命令能夠很方便的進行浮點運算,固然整數運算也再也不話下。手冊頁上說bc是An arbitrary precision calculator language,即一個任意精度的計算語言,注意是一種語言,它提供了一些語法結構,好比條件判斷、循環等,能夠說是很強大的,可是我在實際中尚未找到須要這個用途的場合 。另一個用途就是用來進行進制轉換。html

經常使用參數

通常狀況下,咱們使用不帶任何參數的bc命令。web

bcshell

若是須要bc不輸出提示信息,能夠加上-q參數:windows

bc -qcentos

若是要使用強大的數學庫,好比計算三角函數,須要加上-l參數:bash

bc -l函數

由於bc自己是一個命令解釋器,要退出它只要直接輸入quit回車或者按Ctrl+D終止。ui

使用示例

示例一 命令行方式使用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
81spa

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代碼 複製代碼  收藏代碼
  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 <http://cyberciti.biz/fb/>
# 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 Height

echo -n "Enter base of a triangle : "
read b

echo -n "Enter height of a triangle : "
read h

# calculate it and display back
area=$(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代碼 複製代碼  收藏代碼
  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 <num1> <num2>
# 計算兩個數的和
calc_sum()
{
bc -q <<EOF
$1+$2
EOF
}

# usage: calc_free <count>
# 計算費用,單價0.05元
calc_fee()
{
bc -q <<EOF
0.05*$1
EOF
}
 

 

將以上代碼粘貼到終端。

[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 ~]#

相關文章
相關標籤/搜索