[Jinzan] [Login]
1778

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

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

Full Link //n.sfs.tw/10417

Copy Link [bc] linux 的計算機bc 如何計算帶小數點的指數@新精讚
(Welcome to reprint and indicate the source when reproducing.)
2019-10-22 15:18:06 Last edition
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

You may be interested

[Redhat Linux9] 安裝docker+ docker compose+ apache+ maraidb+ phpmyadmin+ php7 Rocky Linux(Centos9) 下安裝docker及docker compose

[LINUX] 利用 usermod 修改使用者的參數和資料 使用指令 usermod 修改使用者的參數和資料

[Linux] mongodb 初步--2/3 我使用mongo 的一些大小事:叢集

[CentOS] 設定和使用quota quota 的觀念已經很老了,不過每次都會忘記所以寫下來,有需要觀念的朋友請參看鳥哥的網站吧

[CentOS] 改錯fstab 無法開機怎麼辦? Linux 改錯fstab 無法開機怎麼辦?

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

random articles

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

沒有非誰做不可的事,也沒有不可被取代的人 沒有非誰做不可的事,也沒有不可被取代的人

UTF-8 BOM (Byte Order Mark) 的問題 在 Michael Kaplan 那看到 Every character has a story #4: U+feff

[札記] 2016.7~12月札記 札記,只是札記

[JAVA] JWS, JWT, JWE, JOSE是什麼? [JAVA] JWS, JWT, JWE, JOSE是什麼?非常的複雜,儘量來搞清楚..