指數函式 math.exp() 和 cmath.exp()

mathcmath 模組都包含尤拉數:e 並將其與內建的 pow()-function 或**-operator 一起使用,其工作方式大多類似於 math.exp()

import math

math.e ** 2  # 7.3890560989306495
math.exp(2)  # 7.38905609893065

import cmath
cmath.e ** 2 # 7.3890560989306495
cmath.exp(2) # (7.38905609893065+0j)

然而結果是不同的,直接使用指數函式比使用基本 math.e 的內建取冪更可靠:

print(math.e ** 10)       # 22026.465794806703
print(math.exp(10))       # 22026.465794806718
print(cmath.exp(10).real) # 22026.465794806718
#     difference starts here ---------------^