[精讚] [會員登入]
1658

[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

你可能感興趣的文章

[postfix] open /etc/postfix/main.cf: Permission denied 等錯誤 遇到開啟 /etc/postfix/main.cf: Permission denied 等錯誤解決

[Centos7] 新的防火牆firewalld Centos7新的防火牆firewalld 和前面的iptables有很大的不同

[Centos6 7] 掛載磁碟 只是掛載新磁碟

[Centos8] 利用nmcli 設定ipv4 位址 Centos7之後修改網路設定變得比較麻煩一點,此篇介紹使用nmcli指令的方法

[CENTOS7 8] 查看記憶體 linux 中查看記憶體狀態指令整理

[Rocky9] Sphinx 3支援中文 + Docker + PHP7 使用docker建置sphinxsearch 3

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

談借錢 人借錢時手心向上頭向下,人還錢時手心向下頭向上

[Freebsd] 定時測試 ADSL 是否斷線並重連 中華電信 ADSL 雖有固定 ip,可是他卻會不定時「斷線」, 使用以下的 方法可以定時測試是否斷線,以及重新撥接。

為什麼要重造輪子? 什麼輪子?造什麼輪子?我為什麼要重造輪子?

UTF8中文字/全形一覽 快速查詢urf-8的中文字,共計13246中文字(5401常用字+7652罕用字+日文或編號),292全形符號,27半形符號。

[大型機台] 熱血高校躲避球 多少少年時光歲月耗在這個遊戲上,二十幾年前的那個時光,唯一想做的事就是把吃飯錢省下來拿去打一場五塊錢的電動,就算是沒錢也