Linux命令之bc - 浮點計算機、進位轉換

來源:互聯網
上載者:User

標籤:class   http   tar   com   get   使用   

用途說明
Bash內建了對整數四則運算的支援,但是並不支援浮點運算,而bc命令可以很方便的進行浮點運算,當然整數運算也不再話下。手冊頁上說bc是An arbitrary precision calculator language,即一個任意精度的計算語言,注意是一種語言,它提供了一些文法結構,比如條件判斷、迴圈等,可以說是很強大的,但是我在實際中還沒有找到需要這個用途的場合 。另外一個用途就是用來進行進位轉換。
常用參數
一般情況下,我們使用不帶任何參數的bc命令。
bc
如果需要bc不輸出提示資訊,可以加上-q參數:
bc -q
如果要使用強大的數學庫,比如計算三角函數,需要加上-l參數:
bc -l
因為bc本身是一個命令直譯器,要退出它只要直接輸入quit斷行符號或者按Ctrl+D終止。
使用樣本
樣本一 命令列方式使用bc
[[email protected] 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
[[email protected] centos39]#
樣本二 通過管道使用bc來計算
[[email protected] centos39]# echo 3 * 4 | bc
(standard_in) 1: parse error
[[email protected] centos39]# echo "3 * 4" | bc
12
[[email protected] centos39]# echo "scale=7; 355/113" | bc
3.1415929
[[email protected] centos39]#
樣本三 進位轉換
[[email protected] ~]# echo "ibase=16; FFFF" | bc
65535
[[email protected] ~]# echo "obase=16; 1000" | bc
3E8
[[email protected] ~]#
 
 
我們用到bc的ibase和obase方法。
 
  ibase是輸入數位進位,而obase就是輸出數位進位了。很好記,i是input,o是output。
 
  如果用一條命令來轉數字,可以用echo命令和管道結合bc。如下:
 
  10進位轉2進位:
 
  $ echo "obase=2;ibase=10;100" | bc1100100
 
  10進位轉16進位:
 
  $ echo "obase=16;ibase=10;100" | bc64
 
  16進位轉10進位:
 
  $ echo "ibase=16;obase=2;F1" | bc11110001
 
  注意,16進位數位F要大寫,如果小寫結果是不對的。像最上面沒有指定的時候,預設是10進位。
樣本四 將多個運算式寫在一個檔案中一起計算
[[email protected] ~]# cat test.bc
123*321
123/321
scale=4;123/321
 
[[email protected] ~]# 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
[[email protected] ~]#
[[email protected] ~]# cat test.bc | bc
39483
0
.3831
[[email protected] ~]#
樣本五 一個計算三角形面積的Bash指令碼
先複習一下初中的知識:b表示三角形的底,h表示三角形的高,那麼三角形的面積計算公式是b*h/2 。
 
檔案 :area_of_triangle.sh
Bash代碼 
#!/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" 
  
 
[[email protected] academic]# ./area_of_triangle.sh
Enter base of a triangle : 123
Enter height of a triangle : 321
Area of a triangle is 19741.50
[[email protected] academic]#
 樣本六 使用bc命令的指令碼片段
Bash代碼 
# 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 
} 
   
 
將以上代碼粘貼到終端。
[[email protected] ~]# # usage: calc_sum <num1> <num2>
[[email protected] ~]# # 計算兩個數的和
[[email protected] ~]# calc_sum()
> {
> bc -q <<EOF
> $1+$2
> EOF
> }
[[email protected] ~]#
[[email protected] ~]# # usage: calc_free <count>
[[email protected] ~]# # 計算費用,單價0.05元
[[email protected] ~]# calc_fee()
> {
> bc -q <<EOF
> 0.05*$1
> EOF
> }
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# calc_sum 123 321
444
[[email protected] ~]# calc_fee 1000
50.00
[[email protected] ~]#
樣本七 使用數學庫
有文章稱可以計算100位的圓周率pi值。
[[email protected] ~]# echo "scale=100; a(1)*4" | bc
Runtime error (func=(main), adr=11): Function a not defined.
[[email protected] ~]# echo "scale=100; a(1)*4" | bc -l
3.141592653589793238462643383279502884197169399375105820974944592307\
8164062862089986280348253421170676

原文地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.