Python中的數學函式?


在Python中進行簡單到複雜的數學運算(如三角函式、對數運算等),可能需要使用math()模組。

Python的math模組用於訪問數學函式。math()函式的所有方法都用於整數或實數型別物件,但不適用於複數。

要使用此函式,我們需要在程式碼中匯入它。

import math

常量

我們在Python中使用這些常量進行計算:

常量
描述
π (Pi)
返回π的值:3.141592
e
返回自然底數e的值。e ≈ 2.718282
τ (tau)
返回τ的值。τ ≈ 6.283185
inf
返回無限大
nan
非數字型別

數字和數值表示

Python提供不同的函式,用於以不同的形式表示數字,例如:

函式
描述
ceil(x)
返回天花板值,即大於或等於數字x的最小值。
copysign(x, y)
返回數字x,並將y的符號複製到x。
fabs(x)
返回x的絕對值。
factorial(x)
返回x的階乘,其中x >= 0
floor(x)
返回地板值,即小於或等於數字x的最大整數。
fsum(iterable)
返回可迭代物件中元素的總和。
gcd(x,y)
返回x和y的最大公約數。
isfinite(x)
檢查x是否既不是無限大也不是NaN。
isinf(x)
檢查x是否為無限大。
isnan(s)
檢查s是否為非數字。
remainder(x,y)
查詢x除以y後的餘數。

讓我們編寫一個程式來演示上述數學函式的使用:

#Import math library
import math
#Floor and Ceiling
print('The Floor and Ceiling value of 9.45 are:
   ' + str(math.ceil(9.45)) + ', ' + str(math.floor(9.45)))

#Copysign
x = 94
y = -27
print('The value of x after copying the sign from y is: ' + str(math.copysign(x, y)))

#Absolute
print('Absolute value of -94 and 54 are: ' + str(math.fabs(-94)) + ', ' + str(math.fabs(54)))

#Fsum & gcd
my_list = [12, 9.25, 89, 3.02, -75.23, -7.2, 6.3]
print('Sum of the elements of the list: ' + str(math.fsum(my_list)))
print('The GCD of 24 and 56 : ' + str(math.gcd(24, 48)))

#isnan
x = float('nan')
if math.isnan(x):
   print('It is not a number')
      x = float('inf')

#isinf
y = 54
if math.isinf(x):
   print('It is Infinity')
      #x is not a finite number
print(math.isfinite(x))
   #y is a finite number
print(math.isfinite(y))

結果

The Floor and Ceiling value of 9.45 are: 10, 9
The value of x after copying the sign from y is: -94.0
Absolute value of -94 and 54 are: 94.0, 54.0
Sum of the elements of the list: 37.13999999999999
The GCD of 24 and 56 : 24
It is not a number
It is Infinity
False
True

冪與對數函式

這些函式用於計算Python中不同的冪和對數相關任務。

函式
描述
pow(x,y)
返回x的y次冪的值。
sqrt(x)
求x的平方根。
exp(x)
求xe,其中e ≈ 2.718281
log(x[,base])
返回x的對數,其中指定了底數。預設底數為e。
log2(x)
返回x的對數,其中底數為2。
log10(x)
返回x的對數,其中底數為10。

 

演示上述函式用法的示例程式

import math
print("The value of 2^5: " + str(math.pow(2, 5)))
print("Square root of 625: " + str(math.sqrt(625)))
print("The value of 5^e: " + str(math.exp(5)))
print("The value of log(625), base 5: " + str(math.log(625, 5)))
print("The value of log(1024), base 10: " + str(math.log10(1024)))
print("The value of log(1024), base 2: " + str(math.log2(1024)))

結果

The value of 2^5: 32.0
Square root of 625: 25.0
The value of 5^e: 148.4131591025766
The value of log(625), base 5: 4.0
The value of log(1024), base 10: 3.010299956639812
The value of log(1024), base 2: 10.0

三角函式和角度轉換函式

這些函式用於計算不同的三角運算:

函式
描述
sin(x)
返回x的正弦值(x為弧度)。
cos(x)
返回x的餘弦值(x為弧度)。
tan(x)
返回x的正切值(x為弧度)。
asin(x)
返回反正弦值,類似地,還有acos和atan。
degrees(x)
將角度x從弧度轉換為度。
radians(x)
將角度x從度轉換為弧度。

演示上述函式用法的示例程式

import math
print("The value of sin(45 degree): " + str(math.sin(math.radians(45))))
print('The value of cos(pi): ' + str(math.cos(math.pi)))
print("The value of tan(45 degree): " + str(math.tan(math.pi/2)))
print("the angle of sin(0.95504050560):" + str(math.degrees(math.sin(0.95504050560))))

結果

The value of sin(45 degree): 0.7071067811865475
The value of cos(pi): -1.0
The value of tan(45 degree): 1.633123935319537e+16
the angle of sin(0.95504050560):46.77267256206895

更新於:2019年7月30日

2K+ 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告