Python math.expm1() 方法



Python 的 math.expm1() 方法用於計算 ex − 1 的值,其中 e 是自然對數的底數(尤拉數),x 是輸入引數。它計算輸入值的指數減 1。

在數學上,math.expm1() 方法等價於 ex − 1,其中 e 大約等於 2.71828。

例如,如果 x = 1,則 math.expm1(1) 返回 e1 − 1,簡化為 e − 1。

語法

以下是 Python math.expm1() 方法的基本語法:

math.expm1(x)

引數

此方法接受整數或浮點數作為引數,表示 e 的指數。

返回值

該方法返回 e 的 x 次冪減 1。返回值為浮點數。

示例 1

在下面的示例中,我們計算 e 的 1 次冪減 1,這意味著將正整數指數作為引數傳遞給底數 e:

import math
result = math.expm1(1)
print("The result obtained is:", result)  

輸出

獲得的輸出如下:

The result obtained is: 1.718281828459045

示例 2

在這裡,我們將負整數指數作為引數傳遞給底數 e。我們計算 e 的 -2 次冪減 1:

import math
result = math.expm1(-2)
print("The result obtained is:", result)  

輸出

以上程式碼的輸出如下:

The result obtained is: -0.8646647167633873

示例 3

在這個示例中,我們將分數指數作為引數傳遞給底數 e。我們計算 e 的 1.5 次冪減 1:

import math
result = math.expm1(1.5)
print("The result obtained is:", result) 

輸出

我們得到如下所示的輸出:

The result obtained is: 3.481689070338065

示例 4

現在,我們使用變數“x”來儲存指數值。然後我們計算 e 的“x”次冪減 1,即 e2 - 1:

import math
x = 2
result = math.expm1(x)
print("The result obtained is:", result)  

輸出

產生的結果如下所示:

The result obtained is: 6.38905609893065
python_maths.htm
廣告