Python - AI 助手

Python cmath.e 常量



Python 的 cmath.e 常量返回尤拉數 (e = 2.718281828459045)。它是一個預定義的值,通常用於數學計算,每個計算都有其自身的指數增長,例如複利、人口增長和其他工程問題。

尤拉常數無法表示為分數,因為它是一個無理數,其十進位制表示無限不迴圈。

語法

以下是 Python cmath.e 常量的基本語法:

cmath.e

返回值

此常量返回數學常數 e 的值。

示例 1

在下面的示例中,我們使用 Python cmath.e 常量來計算複利。它將在特定時間段內對本金進行計算。

這涉及應用連續複利的複利公式,其中尤拉數被提升到利率乘以時間的冪。

import cmath
p = 2000 #principal
r = 0.03 #rate
t = 6    #time
compound_interest = p * cmath.e ** (r * t)
print("The compound interest after", t, "year is:", compound_interest)

輸出

結果將如下所示:

The compound interest after 6 year is: 2394.4347262436204

示例 2

在這裡,我們使用尤拉數 (e) 計算某個量隨時間的指數增長。這是為了使用 cmath.e 常量計算特定時間段後的最終金額,提供初始金額、增長率和時間。

import cmath
x = 40    #initial_amount
y = 0.4   #growth_rate
t = 4     #time
result = x * cmath.e ** (y * t)
print("The final amount after", t, "years of exponential growth is:", result)

輸出

獲得的輸出如下:

The final amount after 4 years of exponential growth is: 198.1212969758046

示例 3

在這裡,我們使用 cmath.e 常量計算標準分佈中指定點“x”處的機率密度。

import cmath
x = 2
mu = 0
sigma = 1
probability_density = (1 / (cmath.sqrt(2 * cmath.pi) * sigma)) * cmath.e ** (-0.5 * ((x - mu) / sigma) ** 2)
print("The probability density at x =", x, "for a standard normal distribution is:", probability_density)

輸出

我們將獲得如下所示的輸出:

The probability density at x = 2 for a standard normal distribution is: (0.05399096651318806+0j)
python_modules.htm
廣告