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 次方,这意味着将正整数指数作为参数传递给底数 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