Python程式中的對數函式


在本教程中,我們將學習math模組中的對數函式。我們有四種類型的對數函式。Python的math模組提供了所有這些函式。讓我們逐一學習它們。

math.log(number, [Base])

math.log(number, [Base]) 方法用於計算任何Base的對數。如果我們沒有指定任何基數,則它將以e為預設基數。

注意 - 如果您向方法傳遞負數,則會得到ValueError。

示例

讓我們看一些例子。

 線上演示

# importing math module
import math
# logarithm with base 3
print(math.log(15, 7))

輸出

如果您執行上面的程式,您將得到以下結果。

1.3916625094004957

您可以在上面的程式中指定任何您想要的基數。讓我們看看沒有基數的相同示例。預設基數是e

示例

 線上演示

# importing math module
import math
# logarithm with base e(default)
print(math.log(15))

輸出

如果您執行上面的程式碼,您將得到以下結果。

2.70805020110221

示例

讓我們看看如果我們將負數傳遞給math.log()方法會發生什麼。

 線上演示

# importing math module
import math
# logarithm with negative number
print(math.log(-15))

輸出

如果您執行上面的程式,您將得到以下結果。

---------------------------------------------------------------------------
ValueError                   Traceback (most recent call last)
<ipython-input-6-b686fcb806c6> in <module>
      3
      4 # logarithm with base e(default)
----> 5 print(math.log(-15))
ValueError: math domain error

math.log2(number)

如果您想計算以2為底的對數,則可以使用math.log2()方法。它與上述方法類似。讓我們看一些例子。

示例

 線上演示

# importing math module
import math
# logarithm with base 2
print(math.log2(15))

輸出

如果您執行上面的程式碼,您將得到以下結果。

3.9068905956085187

math.log方法類似,如果我們將負數傳遞給math.log2方法,我們將得到錯誤。讓我們用例子來看一下。

示例

 線上演示

# importing math module
import math
# logarithm with base 2 & negative number
print(math.log2(-15))

輸出

如果您透過執行程式檢視程式的輸出,您會發現現在和之前得到的錯誤是相同的。

---------------------------------------------------------------------------
ValueError                               Traceback (most recent call last)
<ipython-input-3-8019b45e571f> in <module>
      3
      4 # logarithm with base 2 & negative number
----> 5 print(math.log2(-15))
ValueError: math domain error

math.log10(number)

我們可以使用math.log10方法找到以10為底的對數。它與上面的math.log2方法類似。讓我們看一些例子。

示例

 線上演示

# importing math module
import math
# logarithm with base 10
print(math.log10(15))

輸出

如果您執行上面的程式,您將得到以下輸出。

1.1760912590556813

嘗試將負數傳遞給math.log10方法。您將得到與上述方法類似的錯誤。

示例

 線上演示

# importing math module
import math
# logarithm with base 10 & negative number
print(math.log10(-15))

輸出

如果您檢視輸出,您將得到以下錯誤。

---------------------------------------------------------------------------
ValueError                            Traceback (most recent call last)
<ipython-input-5-52ac56b802ca> in <module>
      3
      4 # logarithm with base 10 & negative number
----> 5 print(math.log10(-15))
ValueError: math domain error

math.log1p(number)

math.log1p(x)方法將計算以e為底的log(1 + x)。它透過向給定數字新增1來計算其對數。讓我們看一些例子。

示例

 線上演示

# importing math module
import math
# logarithm
print(math.log1p(15)) # similar to math.log(16)

輸出

如果您執行上面的程式,您將得到以下結果。

2.772588722239781

嘗試將負數傳遞給math.log1p方法。我相信您會得到與之前看到的相同的錯誤。

示例

 線上演示

# importing math module
import math
# logarithm
print(math.log1p(-15))

# 匯入math模組 import math # 對數 print(math.log1p(-15))

輸出

由於我們將負數傳遞給了該方法,我們將得到以下錯誤。

---------------------------------------------------------------------------
ValueError                      Traceback (most recent call last)
<ipython-input-15-26016884cb23> in <module>
      3
      4 # logarithm
----> 5 print(math.log1p(-15))
ValueError: math domain error

結論

我們已經看到了math模組中的四個對數方法。如果我們將負數傳遞給本教程中看到的任何對數方法,我們將得到錯誤。您也可以將浮點數傳遞給這些方法。嘗試使用浮點數執行本教程中看到的示例。

更新於:2020年4月24日

390 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.