Python math 模块提供了许多对浮点数的数学运算函数。
math 模块下的函数,return 值均为浮点数,除非另有明确说明。
如果尊敬的读者您需要计算复数,请使用 cmath 模块中的同名函数。
要使用 math 函数必须先导入:
import math
查看 math 模块中的内容:
>>> 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']
常量 | 描述 |
---|---|
math.e | return 欧拉数 (2.7182...) |
math.inf | return 正无穷大浮点数 |
math.nan | return 一个浮点值 NaN (not a number) |
math.pi | π 一般指圆周率。 圆周率 PI (3.1415...) |
math.tau | 数学常数 τ = 6.283185...,精确到可用精度。Tau 是一个圆周常数,等于 2π,圆的周长与半径之比。 |
方法 | 描述 |
---|---|
math.acos(x) | return x 的反余弦,结果范围在 0 到 pi 之间。 |
math.acosh(x) | return x 的反双曲余弦值。 |
math.asin(x) | return x 的反正弦值,结果范围在 -pi/2 到 pi/2 之间。 |
math.asinh(x) | return x 的反双曲正弦值。 |
math.atan(x) | return x 的反正切值,结果范围在 -pi/2 到 pi/2 之间。 |
math.atan2(y, x) | return 给定的 X 及 Y 坐标值的反正切值,结果是在 -pi 和 pi 之间。 |
math.atanh(x) | return x 的反双曲正切值。 |
math.ceil(x) | 将 x 向上舍入到最接近的整数 |
math.netb(n, k) | return 不重复且无顺序地从 n 项中选择 k 项的方式总数。 |
math.copysign(x, y) | return 一个基于 x 的绝对值和 y 的符号的浮点数。 |
math.cos() | return x 弧度的余弦值。 |
math.cosh(x) | return x 的双曲余弦值。 |
math.degrees(x) | 将角度 x 从弧度转换为度数。 |
math.dist(p, q) | return p 与 q 两点之间的欧几里得距离,以一个坐标序列(或可迭代对象)的形式给出。 两个点必须具有相同的维度。 |
math.erf(x) | return 一个数的误差函数 |
math.erfc(x) | return x 处的互补误差函数 |
math.exp(x) | return e 的 x 次幂,Ex, 其中 e = 2.718281... 是自然对数的基数。 |
math.expm1() | return Ex - 1, e 的 x 次幂,Ex,其中 e = 2.718281... 是自然对数的基数。这通常比 math.e ** x 或 pow(math.e, x) 更精确。 |
math.fabs(x) | return x 的绝对值。 |
math.factorial(x) | return x 的阶乘。 如果 x 不是整数或为负数时则将引发 ValueError。 |
math.floor() | 将数字向下舍入到最接近的整数 |
math.fmod(x, y) | return x/y 的余数 |
math.frexp(x) | 以 (m, e) 对的形式return x 的尾数和指数。 m 是一个浮点数, e 是一个整数,正好是 x == m * 2**e 。 如果 x 为零,则return (0.0, 0) ,否则return 0.5 <= abs(m) < 1 。 |
math.fsum(iterable) | return 可迭代对象 (元组, 数组, 列表, 等)中的元素总和,是浮点值。 |
math.gamma(x) | return x 处的伽马函数值。 |
math.gcd() | return 给定的整数参数的最大公约数。 |
math.hypot() | return 欧几里得范数,sqrt(sum(x**2 for x in coordinates))。 这是从原点到坐标给定点的向量长度。 |
math.isclose(a,b) | 检查两个值是否彼此接近,若 a 和 b 的值比较接近则return True,否则return False。。 |
math.isfinite(x) | 判断 x 是否有限,如果 x 既不是无穷大也不是 NaN,则return True ,否则return False 。 |
math.isinf(x) | 判断 x 是否是无穷大,如果 x 是正或负无穷大,则return True ,否则return False 。 |
math.isnan() | 判断数字是否为 NaN,如果 x 是 NaN(不是数字),则return True ,否则return False 。 |
math.isqrt() | 将平方根数向下舍入到最接近的整数 |
math.ldexp(x, i) | return x * (2**i) 。 这基本上是函数 math.frexp() 的反函数。 |
math.lgamma() | return 伽玛函数在 x 绝对值的自然对数。 |
math.log(x[, base]) | 使用一个参数,return x 的自然对数(底为 e )。 |
math.log10(x) | return x 底为 10 的对数。 |
math.log1p(x) | return 1+x 的自然对数(以 e 为底)。 |
math.log2(x) | return x 以 2 为底的对数 |
math.perm(n, k=None) | return 不重复且有顺序地从 n 项中选择 k 项的方式总数。 |
math.pow(x, y) | 将return x 的 y 次幂。 |
math.prod(iterable) | 计算可迭代对象中所有元素的积。 |
math.radians(x) | 将角度 x 从度数转换为弧度。 |
math.remainder(x, y) | return IEEE 754 风格的 x 除于 y 的余数。 |
math.sin(x) | return x 弧度的正弦值。 |
math.sinh(x) | return x 的双曲正弦值。 |
math.sqrt(x) | return x 的平方根。 |
math.tan(x) | return x 弧度的正切值。 |
math.tanh(x) | return x 的双曲正切值。 |
math.trunc(x) | return x 截断整数的部分,即return 整数部分,删除小数部分 |
第1章 Python3 教程 | 第2章 Python3 简介教程 |
---|---|
第3章 Python3 环境搭建教程 | 第4章 Python3 VScode教程 |
第5章 Python3 基础语法教程 | 第6章 Python3 基本数据类型教程 |
第7章 Python3 数据类型转换教程 | 第8章 Python3 推导式教程 |
第9章 Python3 解释器教程 | 第10章 Python3 注释教程 |
第11章 Python3 运算符教程 | 第12章 Python3 数字(Number)教程 |
第13章 Python3 字符串教程 | 第14章 Python3 列表教程 |
第15章 Python3 元组教程 | 第16章 Python3 字典教程 |
第17章 Python3 集合教程 | 第18章 Python3 编程第一步教程 |
第19章 Python3 条件控制教程 | 第20章 Python3 循环语句教程 |
第21章 Python3 迭代器与生成器教程 | 第22章 Python3 函数教程 |
第23章 Python3 数据结构教程 | 第24章 Python3 模块教程 |
第25章 Python3 输入和输出教程 | 第26章 Python3 File教程 |
第27章 Python3 OS教程 | 第28章 Python3 错误和异常教程 |
第29章 Python3 面向对象教程 | 第30章 Python3 命名空间/作用域教程 |
第31章 Python3 标准库概览教程 | 第32章 Python3 实例教程 |
第33章 Python 测验教程 | 第34章 Python3 正则表达式教程 |
第35章 Python3 CGI编程教程 | 第36章 Python3 MySQL(mysql-connector)教程 |
第37章 Python3 MySQL(PyMySQL)教程 | 第38章 Python3 网络编程教程 |
第39章 Python3 SMTP发送邮件教程 | 第40章 Python3 多线程教程 |
第41章 Python3 XML 解析教程 | 第42章 Python3 JSON教程 |
第43章 Python3 日期和时间教程 | 第44章 Python3 内置函数教程 |
第45章 Python3 MongoDB教程 | 第46章 Python3 urllib教程 |
第47章 Python uWSGI 安装配置教程 | 第48章 Python3 pip教程 |
第49章 Python3 operator教程 | 第50章 Python math教程 |
第51章 Python requests教程 | 第52章 Python random教程 |
第53章 Python3 os.replace() 方法教程 |
本文有 phlcsdn2023 原创,欢迎点赞、转载,博客地址:https://blog.csdn.net/phlcsdn2023
- 只要你确信自己正确就去做,做了有人说不好,不做还是有人说不好。相信自己的判断,然后努力去做。
- 有些事,需要自己去悟。一个人并不缺少机会,而是缺乏把握机会的能力。机会:你看不懂、他看不懂、总有人看得懂。事业:你不做、他不做、总有人做。
- 没有人天生幸运。即使是那些天赋异禀的人,也会有力不从心的时刻,也会在某些时候承受各种压力。而他们终于能够抵达终点,是因为他们能够在来自四面八方的质疑声中埋头挺进,哪怕咬着牙,也会一步一个脚印地向前走去。
- 假如时光倒流…我绝对不会让你离开我,绝对会在那刻抱紧你的…哎都怪我当时没有行动,只是伤心的坐着,任你说出分手。
- 少一些拥有欲,多一些行动,拥有欲越大,付出的行动越少。