小白教程
所有教程
关于
Search
172.70.178.82
172.70.178.82
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python math 模块”的源代码
本页内容
上一节:
Python3_operator_模块
下一节:
Python_requests_模块
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Python math}}[[Category:Python 教程|50]] = Python math 模块 = Python '''math''' 模块提供了许多对浮点数的数学运算函数。 '''math''' 模块下的函数,返回值均为浮点数,除非另有明确说明。 如果你需要计算复数,请使用 cmath 模块中的同名函数。 要使用 math 函数必须先导入: <sample title="" desc="" lang="python" hererun="1"> import math </sample> 查看 math 模块中的内容: <sample title="" desc="" lang="python" hererun="1"> >>> import math >>> dir(math) ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp'] </sample> === math 模块常量 === {| class="table table-striped table-hover" ! 常量 ! 描述 |- | [[ref math e|math.e]] | 返回欧拉数 (2.7182...) |- | [[ref math inf|math.inf]] | 返回正无穷大浮点数 |- | [[ref math nan|math.nan]] | 返回一个浮点值 NaN (not a number) |- | [[ref math pi|math.pi]] | π 一般指圆周率。 圆周率 PI (3.1415...) |- | [[ref math tau|math.tau]] | 数学常数 τ = 6.283185...,精确到可用精度。Tau 是一个圆周常数,等于 2π,圆的周长与半径之比。 |} === math 模块方法 === {| class="table table-striped table-hover" ! 方法 ! 描述 |- | [[ref math acos|math.acos(x)]] | 返回 x 的反余弦,结果范围在 0 到 pi 之间。 |- | [[ref math acosh|math.acosh(x)]] | 返回 x 的反双曲余弦值。 |- | [[ref math asin|math.asin(x)]] | 返回 x 的反正弦值,结果范围在 -pi/2 到 pi/2 之间。 |- | [[ref math asinh|math.asinh(x)]] | 返回 x 的反双曲正弦值。 |- | [[ref math atan|math.atan(x)]] | 返回 x 的反正切值,结果范围在 -pi/2 到 pi/2 之间。 |- | [[ref math atan2|math.atan2(y, x)]] | 返回给定的 X 及 Y 坐标值的反正切值,结果是在 -pi 和 pi 之间。 |- | [[ref math atanh|math.atanh(x)]] | 返回 x 的反双曲正切值。 |- | [[ref math ceil|math.ceil(x)]] | 将 x 向上舍入到最接近的整数 |- | [[ref math comb|math.comb(n, k)]] | 返回不重复且无顺序地从 n 项中选择 k 项的方式总数。 |- | [[ref math copysign|math.copysign(x, y)]] | 返回一个基于 x 的绝对值和 y 的符号的浮点数。 |- | [[ref math cos|math.cos()]] | 返回 x 弧度的余弦值。 |- | [[ref math cosh|math.cosh(x)]] | 返回 x 的双曲余弦值。 |- | [[ref math degrees|math.degrees(x)]] | 将角度 x 从弧度转换为度数。 |- | [[ref math dist|math.dist(p, q)]] | 返回 p 与 q 两点之间的欧几里得距离,以一个坐标序列(或可迭代对象)的形式给出。 两个点必须具有相同的维度。 |- | [[ref math erf|math.erf(x)]] | 返回一个数的误差函数 |- | [[ref math erfc|math.erfc(x)]] | 返回 x 处的互补误差函数 |- | [[ref math exp|math.exp(x)]] | 返回 e 的 x 次幂,Ex, 其中 e = 2.718281... 是自然对数的基数。 |- | [[ref math expm1|math.expm1()]] | 返回 Ex - 1, e 的 x 次幂,Ex,其中 e = 2.718281... 是自然对数的基数。这通常比 math.e ** x 或 pow(math.e, x) 更精确。 |- | [[ref math fabs|math.fabs(x)]] | 返回 x 的绝对值。 |- | [[ref math factorial|math.factorial(x)]] | 返回 x 的阶乘。 如果 x 不是整数或为负数时则将引发 ValueError。 |- | [[ref math floor|math.floor()]] | 将数字向下舍入到最接近的整数 |- | [[ref math fmod|math.fmod(x, y)]] | 返回 x/y 的余数 |- | [[ref math frexp|math.frexp(x)]] | 以 (m, e) 对的形式返回 x 的尾数和指数。 m 是一个浮点数, e 是一个整数,正好是 x == m * 2**e 。 如果 x 为零,则返回 (0.0, 0) ,否则返回 0.5 <= abs(m) < 1 。 |- | [[ref math fsum|math.fsum(iterable)]] | 返回可迭代对象 (元组, 数组, 列表, 等)中的元素总和,是浮点值。 |- | [[ref math gamma|math.gamma(x)]] | 返回 x 处的伽马函数值。 |- | [[ref math gcd|math.gcd()]] | 返回给定的整数参数的最大公约数。 |- | [[ref math hypot|math.hypot()]] | 返回欧几里得范数,sqrt(sum(x**2 for x in coordinates))。 这是从原点到坐标给定点的向量长度。 |- | [[ref math isclose|math.isclose(a,b)]] | 检查两个值是否彼此接近,若 a 和 b 的值比较接近则返回 True,否则返回 False。。 |- | [[ref math isfinite|math.isfinite(x)]] | 判断 x 是否有限,如果 x 既不是无穷大也不是 NaN,则返回 True ,否则返回 False 。 |- | [[ref math isinf|math.isinf(x)]] | 判断 x 是否是无穷大,如果 x 是正或负无穷大,则返回 True ,否则返回 False 。 |- | [[ref math isnan|math.isnan()]] | 判断数字是否为 NaN,如果 x 是 NaN(不是数字),则返回 True ,否则返回 False 。 |- | [[ref math isqrt|math.isqrt()]] | 将平方根数向下舍入到最接近的整数 |- | [[ref math ldexp|math.ldexp(x, i)]] | 返回 x * (2**i) 。 这基本上是函数 [[ref math frexp|math.frexp() 的反函数。]] |- | [[ref math lgamma|math.lgamma()]] | 返回伽玛函数在 x 绝对值的自然对数。 |- | [[ref math log|math.log(x[, base])]] | 使用一个参数,返回 x 的自然对数(底为 e )。 |- | [[ref math log10|math.log10(x)]] | 返回 x 底为 10 的对数。 |- | [[ref math log1p|math.log1p(x)]] | 返回 1+x 的自然对数(以 e 为底)。 |- | [[ref math log2|math.log2(x)]] | 返回 x 以 2 为底的对数 |- | [[ref math perm|math.perm(n, k=None)]] | 返回不重复且有顺序地从 n 项中选择 k 项的方式总数。 |- | [[ref math pow|math.pow(x, y)]] | 将返回 x 的 y 次幂。 |- | [[ref math prod|math.prod(iterable)]] | 计算可迭代对象中所有元素的积。 |- | [[ref math radians|math.radians(x)]] | 将角度 x 从度数转换为弧度。 |- | [[ref math remainder|math.remainder(x, y)]] | 返回 IEEE 754 风格的 x 除于 y 的余数。 |- | [[ref math sin|math.sin(x)]] | 返回 x 弧度的正弦值。 |- | [[ref math sinh|math.sinh(x)]] | 返回 x 的双曲正弦值。 |- | [[ref math sqrt|math.sqrt(x)]] | 返回 x 的平方根。 |- | [[ref math tan|math.tan(x)]] | 返回 x 弧度的正切值。 |- | [[ref math tanh|math.tanh(x)]] | 返回 x 的双曲正切值。 |- | [[ref math trunc|math.trunc(x)]] | 返回 x 截断整数的部分,即返回整数部分,删除小数部分 |}
返回至“
Python math 模块
”。
上一节:
Python3_operator_模块
下一节:
Python_requests_模块