加载中...

math模块

math模块

Python的math模块提供了一系列的数学函数和常数。

import math

ceil: 向上取整操作

print(math.ceil(3.01))  # 输出:4
print(math.ceil(-3.45))  # 输出:-3

floor: 向下取整操作

print(math.floor(3.99))  # 输出:3
print(math.floor(-3.99))  # 输出:-4

pow: 计算一个数值的N次方(结果为浮点数)

print(math.pow(2, 3))  # 输出:8.0

sqrt: 开平方运算(结果为浮点数)

print(math.sqrt(9))  # 输出:3.0

fabs: 计算一个数值的绝对值(结果为浮点数)

print(math.fabs(-1))  # 输出:1.0

modf: 将一个数值拆分为整数和小数两部分组成元组

print(math.modf(3.897))  # 输出:(0.8969999999999998, 3.0)

copysign: 将第二个数值的正负号拷贝给第一个(结果为浮点数)

print(math.copysign(-12, -9.1))  # 输出:-12.0

fsum: 计算一个容器数据中的数据的和(结果为浮点数)

lst = [1, 2, 3, 4]
print(math.fsum(lst))  # 输出:10.0

圆周率常数 pi

print(math.pi)  # 输出:3.141592653589793
posted @ 2024-03-03 21:42  江寒雨  阅读(42)  评论(0)    收藏  举报