Python 數學常數 e



Python 的math.e表示數學常數e,其近似值為2.71828。它是Python的math模組中預定義的值,通常用於涉及指數增長和衰減的數學計算,例如複利、人口增長以及各種科學和工程問題。

在一般的數學中,e是一個特殊的數字,稱為尤拉數,以瑞士數學家萊昂哈德·尤拉的名字命名。它是一個無理數,這意味著它的十進位制表示無限地延續而不重複。

常數e是自然對數的底數,經常用於數學計算。它表示(1 + 1/n)n當n趨於無窮大時的極限。

語法

以下是Python math.e常數的基本語法:

math.e

返回值

該常數返回數學常數e的值。

示例 1

在下面的示例中,我們使用math.e常數來計算在指定時間段內,給定利率,對本金髮放的複利。這涉及應用連續複利的複利公式,其中尤拉數(e)的底數被提升到利率乘以時間的冪:

import math
principal = 1000
rate = 0.05
time = 5
compound_interest = principal * math.e ** (rate * time)
print("The compound interest after", time, "years is:", compound_interest)

輸出

以下是上述程式碼的輸出:

The compound interest after 5 years is: 1284.0254166877414

示例 2

在這裡,我們使用尤拉數(e)計算數量隨時間的指數增長。這是透過在給定初始數量、增長率和時間的情況下,使用指數增長公式計算一段時間後的最終數量來完成的:

import math
initial_amount = 10
growth_rate = 0.1
time = 3
final_amount = initial_amount * math.e ** (growth_rate * time)
print("The final amount after", time, "years of exponential growth is:", final_amount)

輸出

獲得的輸出如下:

The final amount after 3 years of exponential growth is: 13.498588075760033

示例 3

在這個例子中,我們使用斯特靈公式來近似一個數字的階乘。這涉及將尤拉數(e)提升到該數字階乘加一的自然對數的冪:

import math
n = 5
factorial_approximation = math.e ** (math.lgamma(n + 1))
print("The approximation of", n, "! using Stirling's approximation is:", factorial_approximation)

輸出

產生的結果如下:

The approximation of 5 ! using Stirling's approximation is: 120.00000000000006

示例 4

現在,我們正在使用尤拉數(e)計算標準正態分佈中指定點“x”處的機率密度:

import math

x = 2
mu = 0
sigma = 1
probability_density = (1 / (math.sqrt(2 * math.pi) * sigma)) * math.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
python_maths.htm
廣告