python怎么调用数学函数

43次阅读
没有评论

共计 801 个字符,预计需要花费 3 分钟才能阅读完成。

Python 中调用数学函数需要导入 math 模块,然后使用该模块提供的内置函数。

以下是一些常用的数学函数的调用方法:

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

这些是常用的数学函数的调用方法,math 模块还提供了其他数学函数,可以通过阅读 Python 官方文档或 math 模块的文档了解更多函数的用法。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计801字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)