1. bash里面能夠實現比較簡單的四則運算
echo $((10*20))
注意是 雙括號+ $ 地址符號.?
2. 但是比較復雜的 可能就難以為繼了 比如不支持精度?
3. 所以這里面需要使用 bc 命令來執行相關的操作.?
man 內容:
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
4. 實現簡單的 測試:
使用 bc -l 進入 計算界面:
比如我輸入的 sqrt(100) 就是計算100 的平方根了
但是我沒找到怎么去立方根
還可以計算 自然對數
比如:
l(10) 表示 以 e為底 10 的對數 e(10) 表示 e 的10次方...
5. 但是這樣計算可能不太方便 也可以使用 管道命令執行相應的操作:
求直接三角形的 第三條邊的長度.
echo "ibase=10,obase=2,254" |bc -l -q
?
6. 或者是可以實現 進制的轉換:
echo "ibase=10;obase=2;254" |bc -l -q
?
?7. 總結語法注意點:
每個參數 使用 分號 分隔 可以使用 管道 也可以不使用管道.?
?
8. 數學函數
MATH LIBRARYIf bc is invoked with the -l option, a math library is preloaded and the default scale is set to 20. The math functions will calculate their results to the scale set at the time of their call. The math library defines the following functions:s (x) The sine of x, x is in radians.c (x) The cosine of x, x is in radians.a (x) The arctangent of x, arctangent returns radians.l (x) The natural logarithm of x.e (x) The exponential function of raising e to the value x.j (n,x)The Bessel function of integer order n of x.
so 可以顯示圓周率的 公式是?
echo "scale=10;a(1)*4" |bc -l -q
顯示自然對數的公式
echo "scale=10;e(1)" |bc -l -q
?