1. 前言
math 模块中包含了各种浮点运算函数,包括:
函数
功能
floor
向下取整
ceil
向上取整
pow
指数运算
fabs
绝对值
sqrt
开平方
modf
拆分小数和整数
fsum
计算列表中所有元素的累加和
copysign
复制符号
pi
圆周率
e
自然对数
2. math.floor(n)
函数 math.floor(n) 的功能是对浮点数 n 向下取整,示例如下:
>>> import math
>>> math.floor(1.5)
1
>>> math.floor(2.5)
2
>>> math.floor(-1.5)
-2
>>> math.floor(-2.5)
-3
3. math.ceil(n)
函数 math.ceil(n) 的功能是对浮点数 n 向上取整,示例如下:
>>> import math
>>> math.ceil(1.5)
2
>>> math.ceil(2.5)
3
>>> math.ceil(-1.5)
-1
>