[精讚] [會員登入]
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

你可能感興趣的文章

[Rocky9] codeignitor4+ nginx+ php8.1-fpm + docker compose 這次的目標是把服務裝在docker,裡面放入 nginx 和 php8.1-fpm的 docker container, 並採用codeignitor4。

[Centos7] 使用網路安裝 Centos7 使用網路來安裝Centos7,雖然這不是什麼新鮮事,但可以省你的磁碟空間或光碟片

[Centos7] 安裝php套件管理程式Composer+ Codeignioter3 很多人在用的套件管理程式 Composer+ Codeignioter3+ 指定安裝目錄

TFTP Server 安裝及使用 讓設備的網路設定檔或是韌體經由TFTP拷備出來,操作的方法

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

[Centos7] 安裝phpMyAdmin Centos 7有新的方式可以安裝phpmyadmin,不必再採用過去的tarball安裝法。

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

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

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

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

[jQuery] select 元件的取值及給值 html中的元件select,在jquery中要如何使用?

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