[精讚] [會員登入]
1483

[bc] linux 的計算機bc 如何計算帶小數點的指數

用 bc 來計算帶小數點的指數,得轉個彎才行

分享此文連結 //n.sfs.tw/10417

分享連結 [bc] linux 的計算機bc 如何計算帶小數點的指數@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-22 15:18:06 最後編修
2016-12-11 13:47:46 By 張○○
 

自動目錄

今日想用 bc 來計算帶小數點的指數,例如 2^0.33 (為什麼我總要幹這種事?用WINDOWS的計算機按按就好了不是嗎?那不是重點)

好吧,就然遇到問題了,就來解決一下:

$ bc -l   <== 加上 -l(L的小寫) 的參數,可是使用內建的幾個函數

scale=20
2.88 ^ 0.55
Runtime warning (func=(main), adr=12): non-zero scale in exponent
1

上面企圖計算 2.88的0.55次方,出現了 Runtime 的警告,同時答案也不對。

因此認真看了一下男人 man bc:

expr ^ expr

The result of the expression is the value of the first raised to the  second.  The second expression must be an integer.  (If the second expression is not an integer, a warning is generated  and the expression is truncated to get an integer value.)  The scale of the result is scale if the  exponent  is  negative.   If  the exponent  is  positive the scale of the result is the minimum of the scale of the first expression times the value of  the  exponent and the maximum of scale and the scale of the first expression.   (e.g.  scale(a^b)   =   min(scale(a)*b,   max(   scale, scale(a))).)   It should be noted that expr^0 will always return the value of 1.

它說:第二項(指數部分)一定要整數,如果不是整數的話,會無條件捨去小數部分。其他在說精確位數的事。

好吧,竟然不給小數的指數計算,所以只好用別的方法來解決。

假設要計算: a^b

讚!以上例 2.88 ^ 0.55 來算:

只要輸入

e(0.55*l(2.88))
1.78922875125216803973

感謝 jlinkels 給的數學推導

參考資料

[1] http://www.linuxquestions.org/questions/programming-9/bc-and-exponents-containing-decimals-and-fractions-755260/


原文 2011-03-20 21:00:17

延伸閱讀

[1] [bc] linux 的計算機 bc 設定小數位數、計算π、次方根

END

你可能感興趣的文章

使用mutt發信(LINUX直接寄信) mutt是一個簡易的發送程式,學會他就不用每次telnet mail server 煩死人

[Centos7] 安裝sphinx+php7 在centos7上安裝sphinx,在PHP7上運作正確

使用RRDTOOL來繪製流量圖 使用RRDTOOL來製作流量圖的簡略說明

[Centos7] 修改啟動的runlevel centos7的 runlevel修改和前面的版本都不一樣

[Centos8] 安裝及設定docker docker 是一個應用程式的容器,相對於vm是作業系統的容器。此篇整理基本安裝及設定。

[Centos6] 安裝wordpress Centos 6下安裝wordpress,非常簡單

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

使用Google尋找你的手機 這近發現google竟然可以用來找android的手機,而且不需要經過什麼設定或安裝軟體。

[MAC] 截取螢幕畫面的方法 截取螢幕畫面的方法,在MAC中叫作螢幕快照,英文是screenshot

安裝SPHINX支援中文 新版本的 sphinx 和舊版不同,網路上很多範例和教學是不能用的。此文是安裝和設定方法分享

Smarty安裝 smarty 是著名的樣版引擎,非常的好用,用多了突然發現拿掉smarty反而不會寫php了,以下是安裝過程..

[CodeIgniter 3] 資料庫的使用方法整理2/2 CI3 承襲 CI2,有很多的builder class可以用,依各人的使用習慣,有人喜歡一堆sql字串,有人喜歡用helper