文章目录
1、引用math标准库
- 第一种方式:import math
对math标准库中函数采用math.()形式使用
import math
math.ceil(0,2)
11
- 第二种方式:from math import <函数名>
对math标准库中函数可以直接采用<函数名>()形式使用
from math import floor
floor(10.2)
10
2、math 标准库解析
以下展示全部采用第一种引用(import math)
2.1 math标准库包括4个数学常数
常数 | 数学表示 | 描述 |
---|---|---|
math.pi | π | 圆周率,值为3.141592653589793 |
math.e | e | 自然对数,值为2.718281828459045 |
math.inf | ∞ | 正无穷大,负无穷大为-math.inf |
math.nan | 非浮点数标记,NAN(Not a Number) |
例如
e
#表示一个常量
>>> math.e
2.718281828459045
pi
#数字常量,圆周率
>>> print(math.pi)
3.141592653589793
2.2math标准库包括16个数值表示函数
例如
ceil(x, /)
Return the ceiling of x as an Integral.
将X的上限作为积分返回。
This is the smallest integer >= x.
这是最小的整数>=x。
#取大于等于x的最小的整数值,如果x是一个整数,则返回x
>>> math.ceil(1.01)
2
>>> math.ceil(1.99