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
广告
© . All rights reserved.