数学运算模块:Python3.7的math模块与cmath模块
数学运算模块:math与cmath
作者:Shawn
python3.7
文档:
https://docs.python.org/3/library/math.html
https://docs.python.org/3/library/cmath.html
- 数学运算模块:math与cmath
- math模块
- 常规部分
- math.ceil(x)
- math.copysign(x, y)
- math.fabs(x)
- math.factorial(x)
- math.floor(x)
- math.fmod(x, y)
- math.frexp(x)
- math.fsum(iterable)
- math.gcd(a, b)
- math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
- math.isfinite(x)
- math.isinf(x)
- math.isnan(x)
- math.ldexp(x, i)
- math.modf(x)
- math.remainder(x, y)
- math.trunc(x)
- 指数、对数部分
- math.exp(x)
- math.expm1(x)
- math.log(x[, base])
- math.log1p(x)
- math.log2(x)
- math.log10(x)
- math.pow(x, y)
- math.sqrt(x)
- 三角函数部分
- math.degrees(x)与math.radians(x)
- math.acos(x)
- math.asin(x)
- math.atan(x)
- math.atan2(y, x)
- math.cos(x)
- math.sin(x)
- math.tan(x)
- math.hypot(x, y)
- math.acosh(x)
- math.asinh(x)
- math.atanh(x)
- math.cosh(x)
- math.sinh(x)
- math.tanh(x)
- 特殊函数部分
- math.erf(x)
- math.erfc(x)
- math.gamma(x)
- math.lgamma(x)
- 常数部分
- math.pi
- math.e
- math.tau
- math.inf
- math.nan
- 常规部分
- cmath模块
- 坐标转换
- cmath.phase(x)
- cmath.polar(x)
- cmath.rect(r, phi)
- 指数对数部分
- cmath.exp(x)
- cmath.log(x[, base])
- cmath.log10(x)
- cmath.sqrt(x)
- 三角函数部分
- cmath.acos(x)
- cmath.asin(x)
- cmath.atan(x)
- cmath.cos(x)
- cmath.sin(x)
- cmath.tan(x)
- cmath.acosh(x)
- cmath.asinh(x)
- cmath.atanh(x)
- cmath.cosh(x)
- cmath.sinh(x)
- cmath.tanh(x)
- 判断部分
- cmath.isfinite(x)
- cmath.isinf(x)
- cmath.isnan(x)
- cmath.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
- 常数部分
- cmath.pi
- cmath.e
- cmath.tau
- cmath.inf
- cmath.infj
- cmath.nan
- cmath.nanj
- 坐标转换
- math模块
math模块
- 其实不起眼的math里加进去了很多黑科技。
常规部分
math.ceil(x)
- 向上取整
>>> import math
>>> math.ceil(0.0)
0
>>> math.ceil(0.1)
1
>>> math.ceil(41.1)
42
math.copysign(x, y)
- copy符号
- 返回x的绝对值和y的符号(对不起,这是我和他的孩子)
>>> import math
>>> math.copysign(1, -0.0)
-1.0
>>> math.copysign(-1, 0.0)
1.0
>>> math.copysign(-1, -0.0)
-1.0
>>> math.copysign(1, 0)
1.0
math.fabs(x)
- 返回绝对值
- math.fabs()与内置函数abs()的区别:
- fabs需要import math后才能调用,abs可以直接使用
- abs可以用于复数而fabs不可以。
>>> math.fabs(-8)
8.0
>>> abs(-8)
8
>>> abs(1+1.0j)
1.4142135623730951
>>> math.fabs(1+1.0j)
Traceback (most recent call last):File "<pyshell#12>", line 1, in <module>math.fabs(1+1.0j)
TypeError: can't convert complex to float
math.factorial(x)
- 这个函数竟然可以返回x!
- 2乘3竟然等于3!
- (返回x的阶乘)
>>> math.factorial(3)
6
>>> math.factorial(10)
3628800
math.floor(x)
- 向下取整
>>> math.floor(0.0)
0
>>> math.floor(0.1)
0
>>> math.floor(-0.1)
-1
>>> math.floor(42.9)
42
math.fmod(x, y)
- 取模运算
- 与路人运算符号%的区别:
- fmod默认返回浮点数
- 对于x、y符号一致时,%与fmod结果一致
- 但x、y符号不一致时,结果不同
- 详见取模运算
>>> 3%2
1
>>> 3%-2
-1
>>> -3%2
1
>>> 3.1%3
0.10000000000000009
>>> math.fmod(3, 2)
1.0
>>> math.fmod(3, -2)
1.0
>>> math.fmod(-3, 2)
-1.0
>>> math.fmod(3.1, 3)
0.10000000000000009
>>> math.fmod(-7, 4)
-3.0
>>> (-7)%4
1
>>> math.fmod(-7, -4)
-3.0
>>> (-7)%(-4)
-3
math.frexp(x)
- 将x分解为尾数与指数
x = 尾数 * 2^指数
>>> math.frexp(3)
(0.75, 2)
>>> 0.75*2**2
3.0
>>> math.frexp(32)
(0.5, 6)
>>> 0.5*2**6
32.0
>>> math.frexp(-32)
(-0.5, 6)
math.fsum(iterable)
- 功能同内置函数sum(),但精度更高
>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.9999999999999999
>>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0
math.gcd(a, b)
- 一键返回最大公约数
>>> math.gcd(32, 33)
1
>>> math.gcd(32, 34)
2
>>> math.gcd(32, 64)
32
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
“接近”判断
abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)若:
- rel_tol=x
- abs_tol=y
- math.isclose(a, b, rel_tol=x, abs_tol=y)翻译成:
- a与b“接近”的判定条件是:
- a与b的差距是否小于ab较大的那个乘以x
- 或ab差距小于y
- 翻译v2:
- ab,你和你女朋友
- x你们的感情
- y有没有房
- “你和你女朋友会不会结婚取决于你们的感情或你们有没有房。”
- a与b“接近”的判定条件是:
>>> math.isclose(3,5,rel_tol=0.2, abs_tol=0.0)
False
>>> math.isclose(3,5,rel_tol=0.5, abs_tol=0.0)
True
>>> math.isclose(3,5,rel_tol=0.2, abs_tol=2.0)
True
math.isfinite(x)
- 判断x有限或者不是nan
>>> math.isfinite(float('nan'))
False
>>> math.isfinite(float('NaN'))
False
>>> math.isfinite(1)
True
math.isinf(x)
- 判断x是否为正负无限
math.isnan(x)
- 判断x是否为nan
math.ldexp(x, i)
- math.frexp(x)的逆运算
>>> math.frexp(32)
(0.5, 6)
>>> math.ldexp(0.5, 6)
32.0
math.modf(x)
- 分离x的整数和小数部分
>>> math.modf(3.1)
(0.10000000000000009, 3.0)
>>> math.modf(-3.3)
(-0.2999999999999998, -3.0)
math.remainder(x, y)
- 符合IEEE 754标准的求余……
>>> math.remainder(5, 2)
1.0
>>> math.remainder(-7, 4)
1.0
>>> math.remainder(-7, -4)
1.0
>>> math.remainder(7, 4)
-1.0
>>> math.remainder(7, -4)
-1.0
math.trunc(x)
- 截断x的小数部分
>>> math.trunc(32)
32
>>> math.trunc(32.1)
32
>>> math.trunc(32.00000000001)
32
>>> math.trunc(-233.2)
-233
指数、对数部分
math.exp(x)
- e为底的指数
>>> math.exp(0)
1.0
>>> math.exp(1)
2.718281828459045
math.expm1(x)
- math.expm1(x)=exp(x) - 1
>>> math.expm1(0)
0.0
>>> math.expm1(1)
1.718281828459045
math.log(x[, base])
- 对数
>>> math.log(math.e,math.e)
1.0
>>> math.log(1,math.e)
0.0
math.log1p(x)
- math.log1p(x)=math.log(x+1,math.e)
>>> math.log1p(math.e-1)
1.0
>>> math.log1p(0)
0.0
math.log2(x)
- 二为底数
>>> math.log2(4)
2.0
>>> math.log2(1024)
10.0
math.log10(x)
- 十为底数
>>> math.log10(10)
1.0
>>> math.log10(10000)
4.0
math.pow(x, y)
- 指数运算
>>> math.pow(2, 0)
1.0
>>> math.pow(2, 10)
1024.0
math.sqrt(x)
- 平方根运算
>>> math.sqrt(4)
2.0
>>> math.sqrt(16)
4.0
三角函数部分
- 运算函数皆默认以弧度值返回。
math.degrees(x)与math.radians(x)
- 角度与弧度的互换
math.acos(x)
- arc cosine
math.asin(x)
- arc sine
math.atan(x)
- arc tangent
math.atan2(y, x)
- 方便调节圆上计算路径的math.atan(x)
math.cos(x)
- cosine
math.sin(x)
- sine
math.tan(x)
- tangent
math.hypot(x, y)
- 勾股定理求斜边
>>> math.hypot(3, 4)
5.0
math.acosh(x)
- inverse hyperbolic cosine of x
math.asinh(x)
- inverse hyperbolic sine of x
math.atanh(x)
- inverse hyperbolic tangent of x
math.cosh(x)
- hyperbolic cosine of x
math.sinh(x)
- hyperbolic sine of x
math.tanh(x)
- hyperbolic tangent of x
特殊函数部分
math.erf(x)
- 返回x处的误差函数
math.erfc(x)
- 返回x处的互补误差函数
- math.erfc(x)= 1.0-math.erf(x)
math.gamma(x)
- 返回x处的gamma函数
math.lgamma(x)
- 返回x处的自然对数gamma函数
>>> math.log(math.gamma(3))
0.6931471805599453
>>> math.lgamma(3)
0.693147180559945
常数部分
math.pi
- 宇宙真理1
math.e
- 宇宙真理2
math.tau
- 两倍的宇宙真理1
- math.tau=math.pi*2
math.inf
- 正无穷,加个负号变负无穷
- 可以直接用float(‘inf’)表达
>>> math.isinf(float('inf'))
True
>>> math.isinf(math.inf)
True
math.nan
- not a number
- 一般见于上限爆了等神秘操作
- 可直接用float(‘nan’)表达
cmath模块
- cmath模块旨在进行复数运算。
- 这一部分用到的就很少了。
坐标转换
cmath.phase(x)
- 复数的角度
>>> phase(complex(-1.0, 0.0))
3.141592653589793
>>> phase(complex(-1.0, -0.0))
-3.141592653589793
cmath.polar(x)
- 复数转为极坐标
- polar(x)=(abs(x), phase(x))
cmath.rect(r, phi)
- 极坐标转换成普通复数形式
r * (math.cos(phi) + math.sin(phi)*1j)
指数对数部分
cmath.exp(x)
- 复数版本
cmath.log(x[, base])
- 复数版本
cmath.log10(x)
- 复数版本
cmath.sqrt(x)
- 复数版本
三角函数部分
cmath.acos(x)
- 复数版本
cmath.asin(x)
- 复数版本
cmath.atan(x)
- 复数版本
cmath.cos(x)
- 复数版本
cmath.sin(x)
- 复数版本
cmath.tan(x)
- 复数版本
cmath.acosh(x)
- 复数版本
cmath.asinh(x)
- 复数版本
cmath.atanh(x)
- 复数版本
cmath.cosh(x)
- 复数版本
cmath.sinh(x)
- 复数版本
cmath.tanh(x)
- 复数版本
判断部分
cmath.isfinite(x)
- 实虚全有限才为True
cmath.isinf(x)
- 实虚任意一个为infinity即为True
cmath.isnan(x)
- 实虚任意一个为nan即为True
cmath.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
- 同math版本,用abs比较故不受影响
常数部分
cmath.pi
- 同math版本
cmath.e
- 同math版本
cmath.tau
- 同math版本
cmath.inf
- 同math版本
cmath.infj
- inf的虚部版本
complex(0.0, float(‘inf’))
cmath.nan
- 同math版本
cmath.nanj
- nan虚部版本
complex(0.0, float(‘nan’))
数学运算模块:Python3.7的math模块与cmath模块相关推荐
- Python math 模块与 cmath 模块
Python math 模块提供了许多对浮点数的数学运算函数. Python cmath 模块包含了一些用于复数运算的函数. cmath 模块的函数跟 math 模块函数基本一致,区别是 cmath ...
- Python Imaging Library: ImageMath Module(图像数学运算模块)
Python Imaging Library: ImageMath Module(图像数学运算模块) (新版本1.1.6)可以使用ImageMath模块来评估"图像表达式".这个模 ...
- python语言的数学模块是什么_python math数学模块
(原创声明,转载引用需要指明来源) 数学模块math概述 math模块包含常见数学函数,但这些函数不能与复数一起使用. 如果需要支持复数,请使用cmath模块中的同名函数. 1.math模块基础语法 ...
- math python 向上取整_Python的数值基本运算和其它数学运算方法
数值基本运算 支持最基本的数学运算符号:+ - * / % **.取正负+x -x,地板除法//,除法和取模divmod(x, y): 12345678910111213141516171819202 ...
- Python3,我用这种方式讲解python模块,80岁的奶奶都说能理解。建议收藏 ~ ~
Python模块讲解 1.引言 2.python模块详解 2.1 含义 2.2 代码示例 2.3 进阶 3.总结 1.引言 小屌丝:鱼哥,你看天上的月亮越来越圆了. 小鱼:唉~ 又是一年团圆夜,又是一 ...
- JAVA的静态方法的运算_java. util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作。...
java. util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作. /* public static double abs ( double num):获取绝对值 ...
- python把模块装到文件夹中_把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功的解决方案...
把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功 解决办法是: 在该文件夹下加入空文件__init__.py python2会把 ...
- 【Python3爬虫(二)】【urlib.request模块】【付费代理+auth认证】
上一篇:[Python3爬虫(一)][urlib.request模块][urlopen+get请求加参数+header请求头+User-Agent+IP代理] ++++++++++开始线+++++++ ...
- 【Python3爬虫(三)】【urlib.request模块】【cookie+Request】
上一篇:[Python3爬虫(二)][urlib.request模块][付费代理+auth认证] ++++++++++开始线++++++++++++++++++ 文章目录 一. cookie 1.1 ...
- 捋一捋Python中的数学运算math库(上篇)
正式的Python专栏第18篇,同学站住,别错过这个从0开始的文章! 很多学习编程的都多多少少学习了一些数学知识. 学委之前也简单吐槽了 Python中奇葩的round函数! 这篇我们讲讲那些常用的数 ...
最新文章
- NodeJS加MongoDB应用入门
- android ImageButton的图片怎么定义?
- flstudio插件找不到_Eclipse4.17安装spring插件的问题
- C语言的VC开发环境界面介绍,【c语言在vc++6.0中编写界面程序】
- Net设计模式实例之中介者模式(Mediator Pattern)
- poj 1287 Networking 最小生成树 Kruskal Prim
- 【django之admin,单例模式】
- python redis连接池最大连接数_python redis之连接池的原理
- win10必须禁用的服务_Win10系统中这3个无用的设置,奉劝你还是早点关闭吧
- CSS的position属性
- STM32 跑马灯程序设计
- 安装Hadoop3.2.1(很多坑)
- 加州房价篇 (二) : 处理数据
- OPPOR9Plus系列通刷刷机包精简包_OPPOR9Plus线刷包救砖包_OPPOR9Plus纯净刷机包_OPPOR9Plus刷机教程下载
- Linux-dd命令详解
- 写给面临危机的计算机类学生们的肺腑之言 转
- 618新旧巨头战笔记之一
- Linux红帽RedHat 8升级更新Firefox火狐浏览器版本方法
- FlashHelperService.exe flash player流氓广告?
- 《管理的常识》读书笔记