Python数学库 | exp() 方法
Python有数学库并且有很多关于它的函数。一个这样的函数是exp() 。此方法用于计算 e 的幂,即 e^y 或者我们可以说是 y 的指数。 e 的值大约等于 2.71828…..
Syntax : math.exp(y)
Parameters :
y [Required] – It is any valid python number either positive or negative.
Note that if y has value other than number then its return error.
Returns: Returns floating point number by calculating e^y.
代码#1:
# Python3 code to demonstrate
# the working of exp()
import math
# initializing the value
test_int = 4
test_neg_int = -3
test_float = 0.00
# checking exp() values
# with different numbers
print (math.exp(test_int))
print (math.exp(test_neg_int))
print (math.exp(test_float))
输出:
54.598150033144236
0.049787068367863944
1.0
代码#2:
# Python3 code to demonstrate
# the working of exp()
import math
# checking exp() values
# with inbuilt numbers
print (math.exp(math.pi))
print (math.exp(math.e))
输出:
23.140692632779267
15.154262241479262
代码 #3:类型错误
# Python3 code to demonstrate
# the working of exp()
import math
# checking for string
print (math.exp("25"))
输出:
Traceback (most recent call last):
File "/home/c7ae4f1bef0ed8c7756b3f55e7d2ce81.py", line 6, in
print (math.exp("25"))
TypeError: a float is required