溫馨提示×

python怎么調(diào)用數(shù)學(xué)函數(shù)

小億
183
2023-11-08 18:53:41
欄目: 編程語言

Python中調(diào)用數(shù)學(xué)函數(shù)需要導(dǎo)入math模塊,然后使用該模塊提供的內(nèi)置函數(shù)。

以下是一些常用的數(shù)學(xué)函數(shù)的調(diào)用方法:

  1. 平方根函數(shù) - sqrt(x)
import math
result = math.sqrt(16)
print(result)  # 輸出:4.0
  1. 絕對值函數(shù) - fabs(x)
import math
result = math.fabs(-5)
print(result)  # 輸出:5.0
  1. 向上取整函數(shù) - ceil(x)
import math
result = math.ceil(3.7)
print(result)  # 輸出:4
  1. 向下取整函數(shù) - floor(x)
import math
result = math.floor(3.7)
print(result)  # 輸出:3
  1. 四舍五入函數(shù) - round(x)
import math
result = round(3.7)
print(result)  # 輸出:4
  1. 冪函數(shù) - pow(x, y)
import math
result = math.pow(2, 3)
print(result)  # 輸出:8.0
  1. 自然對數(shù)函數(shù) - log(x)
import math
result = math.log(10)
print(result)  # 輸出:2.302585092994046
  1. 指數(shù)函數(shù) - exp(x)
import math
result = math.exp(1)
print(result)  # 輸出:2.718281828459045
  1. 正弦函數(shù) - sin(x)
import math
result = math.sin(math.pi/2)
print(result)  # 輸出:1.0
  1. 余弦函數(shù) - cos(x)
import math
result = math.cos(math.pi)
print(result)  # 輸出:-1.0

這些是常用的數(shù)學(xué)函數(shù)的調(diào)用方法,math模塊還提供了其他數(shù)學(xué)函數(shù),可以通過閱讀Python官方文檔或math模塊的文檔了解更多函數(shù)的用法。

0