math 数学模块

# ### math 数学模块
import math
#ceil()  向上取整操作 (对比内置round)
res = math.ceil(6.0001) # 注意精度损耗
print(res)

#floor() 向下取整操作 (对比内置round)
res = math.floor(3.5)
res = math.floor(3.9999999)
print(res)

#pow()  计算一个数值的N次方(结果为浮点数) (对比内置pow)
res = math.pow(2,3)
# res = math.pow(2,3,3) # math 模块中的pow 只有2个参数
print(res)
# print(pow(2,3))
# print(pow(2,3,5))

#sqrt() 开平方运算(结果浮点数)
res = math.sqrt(9)
print(res)

#fabs() 计算一个数值的绝对值 (结果浮点数) (对比内置abs)
res = math.fabs(-1)
print(res)

#modf() 将一个数值拆分为整数和小数两部分组成元组
res = math.modf(14.9)
print(res)

#copysign()  将参数第二个数值的正负号拷贝给第一个
res = math.copysign(-13,67)
print(res)

#fsum() 将一个容器数据中的数据进行求和运算 (结果浮点数)(对比内置sum)
listvar = [234,242,4,2,42,42,4]
res = math.fsum(listvar)
print(res)

#圆周率常数 pi
res = math.pi
print(res)

  

猜你喜欢

转载自www.cnblogs.com/huangjiangyong/p/10920486.html