[精讚] [會員登入]
1486

[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

你可能感興趣的文章

[Centos7] SSL自簽憑證+APACHE+Selinux SSL的自簽憑證

Linux ext3 系統下刪除檔案救回全記錄 今天一個不小心,下了 rm 的指令,從此奮戰一個下午,終於把檔案救回來,以下是全記錄...

[Centos6] php5.3升級為php5.6 Centos6 + php5.6 升級

[CentOS] 新增修改及刪除使用者 新增、修改、刪除使用者和群組

動態DNS -- dynamic DNS實作 BIND9動態DNS設定

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

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

[jQuery] 利用load()來達成ajax的寫法 jQuery中利用load()來達成ajax的寫法,也有人稱他是假的ajax,作法就是..

[CodeIgniter3] 解決無法上傳特定檔案(.sb2)的問題 上傳時出現The filetype you are attempting to upload is not allowed,要怎麼解決?

世紀帝國征服者新版本--被遺忘的帝國 世紀帝國征服者新版本--被遺忘的帝國 世紀二代的征服者是精典遊戲中的精典,aofe更好玩...

APACHE的記錄檔格式 LogFormat 語法 在APACHE中有定義一些記錄的語法模版 在 /etc/httpd/conf/httpd.conf 中: LogForm