math库概述
- math库是Python 提供的内置数学类函数库
- math库不支持复数类型
- math库一共提供了4个数学常数和44个函数。44个函数共分为4类,包括:16个述职表示函数、8个幂对数函数、16个三角对数函数和4个高等特殊函数
math库引用
首先使用保留字import引用该库
- import math
对math库中函数函数采用math.()形式使用 - from math import <函数名>
对math库中函数可以直接采用<函数名>()形式使用
math库解析
1. math库包括4个数字常数
常数 | 数学表示 | 描述 |
---|---|---|
math.pi | π | 圆周率,值为3. 141592653589793 |
math.e | e | 自然对数,值为2.718281828459045 |
math.inf | ∞ \infty ∞ | 正无穷大,负无穷大为-math.inf |
math.nan | 非浮点数标记,NaN (Not a Number) |
2. math库包括16个述职表示函数
函数 | 数学表示 | 描述 |
---|---|---|
math.fabs(x) | |x| | 返回x的绝对值 |
math.fmod(x, y) | x % y | 返回x与y的模浮点数精确求和 |
math.fsum([xy_…]) | x + y + … | 浮点数精确求和向上取整,返回不小于x的最小整数 |
math.ceil(x) | 向上取整,返回不小于x的最小整数 | |
math.floor(x) | 向下取证,返回不大于x的最大整数 | |
math. factorial(x) | 返回x的阶乘,如果x是小数或负数,返回ValueError | |
math. gcd(a, b) | 返回a与b的最大公约数 | |
math.frepx(x) | x = m ∗ 2 e x = m * 2^{e} x=m∗2e | 返回(m,e),当x=0, 返回(0.0, 0) |
math.ldexp(x, i) | x ∗ 2 i x * 2^{i} x∗2i | 返回x * 2运算值,math fepx(x)函数的反运算 |
math. modf(x) | 返回x的小数和整数部分 | |
math.trunc(x) | 返回x的整数部分 | |
math.copysign(x, y) | |x| * |y| / y | 用数值y的正负号替换数值x的正负号 |
math.isclose(a,b) | 比较a和b的相似性,返回True或False | |
math.isfinite(x) | 当x为无穷大,返回True;否则,返回False | |
math. isinf(x) | 当x为正数或负数无穷大,返回True; 否则,返回False | |
math.isnan(x) | 当x为正数或负数无穷大,返回True; 否则,返回False |
3. math库包括8个幂对数函数
函数 | 数学表示 | 描述 |
---|---|---|
math. pow(x.y) | x y x^{y} xy< |