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∗2ex = m * 2^{e}x=m∗2e | 返回(m,e),当x=0, 返回(0.0, 0) |
| math.ldexp(x, i) | x∗2ix * 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) | xyx^{y}xy | 返回x的y次幂 |
| math.exp(x) | exe^{x}ex | 返回e的x次幂,e是自然对数 |
| math. expml(x) | exe^{x}ex - 1 | 返回e的x次幂减1 |
| math.sqrt(x) | x\sqrt{ x }x | 返回x的平方根 |
| math.log(x[,base]) | logbasexlog_{base}xlogbasex | 返回x的对数值,只输入x时,返回自然对数,即ln x |
| math.log1p(x) | ln(1 + x) | 返回1+x的自然对数值 |
| math.log2(x) | log2xlog_2xlog2x | 返回x的2对数值 |
| math.log10(x) | log10xlog_{10}xlog10x | 返回x的10对数值 |
4.math库包括16个“三角双曲函数”
| 函数 | 数学表示 | 描述 |
|---|---|---|
| math.degree(x) | 角度x的弧度值转角度值 | |
| math.radians(x) | 角度x的角度值转弧度值 | |
| math.hypot(x,y) | x2+y2\sqrt{x^2 + y^2}x2+y2 | 返回(x,y)坐标到原点(0,0)的距离 |
| math. sin(x) | sin x | 返回x的正弦函数值,x是弧度值 |
| math.cos(x) | cos x | 返回x的余弦函数值,x是弧度值 |
| math.tan(x) | tan x | 返回x的正切函数值,x是弧度值 |
| math.asin(x) | arcsin x | 返回x的反正弦函数值,x是弧度值 |
| math.acos(x) | arccos x | 返回x的反余弦函数值,x是弧度值 |
| math.atan(x) | arctan x | 返回x的反正切函数值,x是弧度值 |
| math. atan2(y,x) | arctan y/x | 返回y/x的反正切函数值,x是弧度值 |
| math. sinh(x) | sinh x | 返回x的双曲正弦函数值 |
| math.cosh(x) | cosh x | 返回x的双曲余弦函数值 |
| math. tanh(x) | tanh x | 返回x的双曲正切函数值 |
| math asinh(x) | arcsinh x | 返回x的反双曲正弦函数值 |
| math acosh(x) | arccosh x | 返回x的反双曲余弦函数值 |
| math atanh(x) | arctanh x | 返回x的反双曲正切函数值 |
5.math库包括4个高等特殊函数
| 函数 | 数学函数 | 描述 |
|---|---|---|
| math.erf(x) | 2π{2} \over {\sqrt{π}}π2 ∫0xe−t2dt\int_0^x e^{-t^2}dt∫0xe−t2dt | 高斯误差函数,应用于概率论、统计学等领域 |
| math.erfc(x) | 2π{2} \over {\sqrt{π}}π2 ∫x∞e−t2dt\int_x^\infty e^{-t^2}dt∫x∞e−t2dt | 余补高斯误差函数,math.erfc(x)=1 - math.erf(x) |
| math.gamma(x) | ∫0∞xt−1e−xdx\int_0^\infty x^{t - 1} e^{-x}dx∫0∞xt−1e−xdx | 伽玛(Gamma)函数,也叫欧拉第二积分函数 |
| math. lgamma(x) | ln(gamma(x)) | 伽玛函数的自然对数 |
本文介绍了Python内置的math库,包括库的概述、引用方式以及详细解析了库中的4个数学常数、16个数值运算函数、8个幂对数函数、16个三角双曲函数和4个高等特殊函数。math库不支持复数运算,使用时需通过import导入。

809

被折叠的 条评论
为什么被折叠?



