# _*_ coding: utf-8 _*_
import math
import cmath
#math 模块提供了许多对浮点数的数学运算函数

print(dir(math))
#['__doc__', '__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']

#cmath 模块包含了一些用于复数运算的函数
print(dir(cmath))
#['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan',
# 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10',
# 'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau']

#Number-theoretic and representation functions
#1 math.ceil(x):返回大于x的最小整数,如果x为整数,输出x
print(math.ceil(-9),math.ceil(9),math.ceil(-9.1),math.ceil(9.1)) #-9,9,-9,10

#2 math.comb:用于获取从n个项目中选择k个项目(不重复且无顺序)的方法数量。它本质上评估为n! /(k!*(n-k)!)它也被称为二项式系数,
# 因为它等效于表达式(1 + x)的多项式展开中的k-th项的系数n。n!为阶乘=1x2x3x...x(n-1)
#返回:一个整数值,表示从n个项目中选择k个项目(无重复且无顺序)的方式数量,如果k>n则返回0
n,k=3,2
print(math.comb(n,k))  #n!/(k!*(n-k))=1x2x3/(1x2*(3-2))=6/2=3

#3 math.copysign(x,y):返回|x|的浮点型绝对值,但带有y的符号,比如x=1.0,y=-4,结果为-1.0
print(math.copysign(1.0,-5))  #-1.0

#4 math.fabs(x):返回x的绝对值,与内置函数abs(x)一样

#5 math.factorial(x):Return x factorial as an integer. Raises ValueError if x is not integral or is negative.
# 返回阶乘:n!=1×2×3×……×n 或 n!=n×(n-1)!
print(math.factorial(4)) #1x2x3x4=24

#6 math.floor(x):返回小于等于x的最大整数
print(math.floor(1.5),math.floor(-1.5)) #1,-2

#7 math.fmod(x,y): 浮点数求余,与符号运算符 x%y类似但有差异,x%y用于整型数的求余.
print(math.fmod(5,2))  #1.0

#8 math.frexp(x):Return the mantissa and exponent of x as the pair (m, e).
# m is a float and e is an integer such that x == m * 2**e exactly.
# If x is zero, returns (0.0, 0), otherwise 0.5 <= abs(m) < 1.
# This is used to “pick apart” the internal representation of a float in a portable way
#返回x的尾数和指数作为一对(m, e)。m是一个浮点数,e是一个整数,使x == m * 2**e准确。如果x为0,返回(0.0,0),
# 否则0.5 <= abs(m) < 1。这是用来以可移植的方式“拆分”浮点数的内部表示
print(math.frexp(1)) #(0.5, 1)即m=0.5,e=1=>  0.5*2^1=0.5*2=1
print(math.frexp(10)) #(0.625, 4)

#9 math.fsum(x):浮点型求和,区别于整型求和内置函数sum
print(sum([0.1,0.1,.1,.1,.1,.1,.1,.1,.1,.1]),math.fsum([0.1,0.1,.1,.1,.1,.1,.1,.1,.1,.1])) #0.9999999999999999 1.0

#10 math.gcd(a,b):返回指定整型参数的最大公约数。如果任何参数是非零的,则返回值是所有参数的除数中最大的正整数。
# 如果所有参数都为0,则返回值为0。不带参数的Gcd()返回0。它接受两个整数并返回其最大公约数(将两个整数相除的最大正整数)。数字)。
# gcd=greatest common divisor
print(math.gcd(),math.gcd(10,0),math.gcd(10),math.gcd(10,15)) #0 10 10 5(10和15最大公约数是5)

#11 math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0):如果a和b的值很接近,则返回True,否则返回false。
#两个值是否被认为接近是根据给定的绝对和相对公差确定的。
#rel_tol是相对公差——相对于a或b的较大绝对值,它是a和b之间允许的最大差值。例如,要设置5%的公差,传递rel_tol=0.05。默认容忍度是1e-09,
# 这确保两个值在大约9位十进制数字内是相同的。Rel_tol必须大于零。
#Abs_tol是最小绝对公差-在接近零的比较中有用。Abs_tol必须至少为零。
#如果没有错误发生,结果将是:abs(a-b) <= max( rel_tol * max(abs(a),abs(b)),  abs_tol  )。
print(math.isclose(0.9,1.0,rel_tol=0.05,abs_tol=0))
# abs(0.9-1.0)=0.1, max(0.05*max(0.9,1.0),0)=max(0.05,0)=0.05  左边大于右边,所以返回false

#12 math.isinf(x): 如果x是正无穷或负无穷则返回True,否则返回false
#13 math.isfinite(x):如果x既不是无穷大也不是NaN则返回True,否则返回false。(注意,0.0被认为是有限的。)
#  math.inf 为常数,等效于math.inf=float('inf')
print(math.isinf(float('inf')),math.isfinite(10))  #True True

#14 math.isnan(x):Return True if x is a NaN (not a number), and False otherwise,x –检查是否为“ NaN”的数字
print(math.isnan(float('inf')),math.isnan(float('nan')))  #False True

#15 math.isqrt(n):返回非负整数n的平方根。这是确切的平方根n的底,或等价于使a²≤n的最大整数。
#对于某些应用,取n≤a²的最小整数可能更方便,或者换句话说,取n的精确平方根的极值。对于正n,可以用inga = 1 + isqrt(n - 1)来计算。
print(math.isqrt(25),math.isqrt(26))  #5,5(取整数)

#16 math.lcm(a,b):返回指定整数参数的最小公倍数。如果所有参数都是非零的,那么返回值是所有参数的倍数中最小的正整数。
# 如果任何参数为零,则返回值为0。Lcm()不带参数返回1。lcm=least common multiple
print(math.lcm(4,6)) #12

#17 math.ldexp(x, i) :Return x * (2**i). This is essentially the inverse of function frexp().
print(math.ldexp(2,4)) #32.0

#18 math.modf(x):Return the fractional and integer parts of x. Both results carry the sign of x and are floats.
print(math.modf(1.5),math.modf(-1.5)) #(0.5, 1.0) (-0.5, -1.0)

#19 math.nextafter(x, y): Return the next floating-point value after x towards y,If x is equal to y, return y.
#math.nextafter(x, math.inf) goes up: towards positive infinity.
#math.nextafter(x, -math.inf) goes down: towards minus infinity.
#math.nextafter(x, 0.0) goes towards zero.
#math.nextafter(x, math.copysign(math.inf, x)) goes away from zero.

#math.ulp(x):Return the value of the least significant bit of the float x:
#If x is a NaN (not a number), return x.
#If x is negative, return ulp(-x).
#If x is a positive infinity, return x.
#If x is equal to zero, return the smallest positive denormalized representable float
# (smaller than the minimum positive normalized float, sys.float_info.min).
#If x is equal to the largest positive representable float, return the value of the least significant bit of x,
# such that the first float smaller than x is x - ulp(x).
#Otherwise (x is a positive finite number), return the value of the least significant bit of x,
# such that the first float bigger than x is x + ulp(x).

#20 math.perm(n,k=None):Return the number of ways to choose k items from n items without repetition and with order.
#Evaluates to n! / (n - k)! when k <= n and evaluates to zero when k > n.
#If k is not specified or is None, then k defaults to n and the function returns n!.
#Raises TypeError if either of the arguments are not integers. Raises ValueError if either of the arguments are negative.
#返回从n个项目中选择k个项目的方法个数,不重复且有顺序。等于n !/ (n - k)!当k <= n时,k > n等于零。
#如果k未指定或为None,则k默认为n,函数返回n!如果参数中的任何一个不是整数将引发TypeError。如果两个参数中有一个为负则引发ValueError。
print(math.perm(4),math.perm(4,2))  #4!=24, 4!/(4-2)!=24/2=12

#21 math.prod(x,*,start=1):用于计算给定可迭代对象中所有元素的乘积。 Python中的大多数内置容器(例如list,tuple)都是可迭代的。
# 迭代器必须包含数字值,否则可能会拒绝非数字类型. start为开始被乘数, x*start,默认为1
print(math.prod((1,2,3),start=1),math.prod((1,2,3),start=2)) #1*2*3*1(start=1)=6 , 1*2*3*2(start=2)=12

#22 math.remainder(x,y):返回IEEE 754风格的x关于y的余数。对于有限x和有限非零y,这是差x - n*y,其中n是最接近商x /y的精确值的整数。
# 如果x / y恰好是两个连续整数的中点,则n使用最接近偶数的整数。余数r =余数(x,y)因此总是满足abs(r) <= 0.5 * abs(y)。
print(math.remainder(12.5,2),12.5%2)  #0.5,0.5

#23 math.trunc(x):Return the Real value x truncated to an Integral (usually an integer)
#trunc=truncate截断
print(math.trunc(10.9))  #10 返回截断为integral的Real值x

#Power and logarithmic functions幂函数和对数函数
#1 math.exp(x):Return e raised to the power x, where e = 2.718281… is the base of natural logarithms.
# This is usually more accurate than math.e ** x or pow(math.e, x).返回e的x次方,其中e = 2.718281…是自然对数的底
print(math.exp(1),math.exp(2)) #=e^1=2.718281828459045,7.38905609893065

#2 math.expm1(x): 返回e的x次方,减去1。e是自然对数的底。对于小浮点数x, exp(x) - 1中的减法可能会导致显著的精度损失;
# expm1()函数提供了一种计算此数量到完全精度的方法
print(math.exp(1e-5)-1,math.expm1(1e-5)) #1.0000050000069649e-05 1.0000050000166667e-05

#3 math.log(x[,base])
#有一个参数,返回x的自然对数(以e为底)。e=2.718281828459045
#有两个参数,返回x到给定底的对数,计算为log(x)/log(底)。
print(math.log(2.718281828459045)) #返回1.0=loge(e^1) 默认以e为底数,x=e时即e^1
print(math.log(100,10)) #返回2.0: log10(100)=log10(10^2)=2.0 以y=10为底,x=100的对数

#4 math.log1p(x):Return the natural logarithm of 1+x (base e).
# The result is calculated in a way which is accurate for x near zero
print(math.log1p(0.0000001),math.log(0.0000001)) #9.999999500000032e-08 -16.11809565095832

#5 math.log2(x):Return the base-2 logarithm of x. This is usually more accurate than log(x, 2).
print(math.log2(10.123456),math.log(10.123456,2)) #3.3396299840202284 3.339629984020228

#6 math.log10(x):Return the base-10 logarithm of x. This is usually more accurate than log(x, 10).
print(math.log10(2),math.log(2,10)) #0.3010299956639812 0.30102999566398114

#7 math.pow(x,y):Return x raised to the power y. Exceptional cases follow Annex ‘F’ of the C99 standard
# as far as possible. In particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN.
# If both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises ValueError.
#Unlike the built-in ** operator, math.pow() converts both its arguments to type float.
# Use ** or the built-in pow() function for computing exact integer powers.
print(math.pow(10,2))  #100.0 以x^y次方

#8 math.sqrt(x): Return the square root of x.
print(math.sqrt(10),math.isqrt(10)) #x的开方根:3.1622776601683795  3

#Trigonometric functions三角函数
#1 math.acos(x) : Return the arc cosine of x, in radians. The result is between 0 and pi.
# acos() 返回x的反余弦弧度值,x -- -1到1之间的数值。如果x是大于1,会产生一个错误
print(math.acos(0.5),math.acos(0)) #1.0471975511965979,1.5707963267948966

#2 math.asin(x):Return the arc sine of x, in radians. The result is between -pi/2 and pi/2.
#asin() 返回x的反正弦弧度值,x : -1 到 1 之间的数值。如果 x 是大于 1,会产生一个错误
print(math.asin(0.5)) #0.5235987755982989

#3 matn.atan(x): Return the arc tangent of x, in radians. The result is between -pi/2 and pi/2.
#返回x的反正切弧度值。
print(math.atan(0.8),math.atan(10)) #0.6747409422235527 1.4711276743037347

#4 math.atan2(y,x): Return atan(y / x), in radians. The result is between -pi and pi.
# The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis.
# The point of atan2() is that the signs of both inputs are known to it, so it can compute the
# correct quadrant for the angle. For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4.
#返回给定的 X 及 Y 坐标值的反正切值
print(math.atan2(-1,-1),math.atan(1)) #0.7853981633974483 -0.7853981633974483
print(math.atan2(1,1),math.atan(1)) #0.7853981633974483 0.7853981633974483

#5 math.atanh(x):数字的双曲反正切值作为值返回。此函数中传递的值应在-0.99到0.99之间
print(math.atanh(0.5),math.atan(0.5)) #0.5493061443340549,0.4636476090008061

#6 math.cos(x):Return the cosine of x radians
#返回x的弧度的余弦值,-1 到 1 之间
print(math.cos(math.pi),math.cos(2*math.pi))  #-1.0,1.0

#7 math.sin(x)
print(math.sin(math.pi/2),math.sin(2*math.pi)) #1.0 -2.4492935982947064e-16

#8 math.tan(x)
print(math.tan(math.pi/2)) #1.633123935319537e+16

#9 math.hypot(*coordinates):
# Return the Euclidean norm, sqrt(sum(x**2 for x in coordinates)). This is the length of the vector from
# the origin to the point given by the coordinates.
#For a two dimensional point (x, y), this is equivalent to computing the hypotenuse of a right triangle
# using the Pythagorean theorem, sqrt(x*x + y*y).
#返回欧几里得范数,根号(sum(x**2 for x in坐标))。这是向量从原点到点的长度由坐标给出。
#对于二维点(x, y),这相当于用勾股定理计算直角三角形的斜边,根号(x*x + y*y)
print(math.hypot(10),math.hypot(10,2))  #10.0 10.19803902718557

#10 math.dist(p,q):Return the Euclidean distance between two points p and q, each given as a sequence (or iterable)
# of coordinates. The two points must have the same dimension
#Roughly equivalent to:sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
#返回两点p和q之间的欧氏距离,每个点都以坐标序列(或可迭代)的形式给出。这两个点必须具有相同的维度
print(math.dist([20.0],[2.0]),math.dist([1.0,2.0],[10.0,3.0])) #18.0 9.055385138137417

#Angular conversion:角转换
#角度和弧度关系是:2π 弧度 = 360°。从而 1°≈0.0174533 弧度,1 弧度≈57.29578°。
#1) 角度转换为弧度公式:弧度=角度÷180×π
#2) 弧度转换为角度公式: 角度=弧度×180÷π

#1 math.degrees(x):将弧度转换为角度,如degrees(math.pi/2) , 返回90.0
print(math.degrees(math.pi/2)) #90.0度
#2 math.radians(x):将角度转换为弧度
print(math.radians(90))  #1.5707963267948966

#Hyperbolic functions双曲线函数
#1 math.acosh(x):Return the inverse hyperbolic cosine of x.
#2 math.asinh(x):Return the inverse hyperbolic sine of x.
#3 math.atanh(x):Return the inverse hyperbolic tangent of x.
#4 math.cosh(x):Return the hyperbolic cosine of x.
#5 math.sinh(x):Return the hyperbolic sine of x.
#6 math.tanh(x):Return the hyperbolic tangent of x.

#Special functions:特殊函数
#1 math.erf(x)
#2 math.erfc(x)
#3 math.gamma(x)
#4 math.lgamma(x)

#Constants:常量
#1: math.pi
#2: math.e
#3: math.tau: The mathematical constant τ = 6.283185…, to available precision.
# Tau is a circle constant equal to 2π, the ratio of a circle’s circumference to its radius.
# To learn more about Tau, check out Vi Hart’s video Pi is (still) Wrong,
# and start celebrating Tau day by eating twice as much pie!
#数学常数τ = 6.283185…,达到可用的精度。Tau是一个圆常数,等于2π,圆的周长与半径之比
#4 math.inf=float('inf')
#5 math.nan=float('nan')

python之math_cmath相关推荐

  1. Github配置(git+vscode+python+jupyter)

    ①下载git 打开 git bash 工具的用户名和密码存储 $ git config --global user.name "Your Name" $ git config -- ...

  2. 【实验楼】python简明教程

    ①终端输入python进入 欣赏完自己的杰作后,按 Ctrl + D 输入一个 EOF 字符来退出解释器,你也可以键入 exit() 来退出解释器. ②vim键盘快捷功能分布 ③这里需要注意如果程序中 ...

  3. 【Kaggle Learn】Python 5-8

    五. Booleans and Conditionals Using booleans for branching logic x = True print(x) print(type(x))''' ...

  4. 【Kaggle Learn】Python 1-4

    [Kaggle Learn]Python https://www.kaggle.com/learn/python 一. Hello, Python A quick introduction to Py ...

  5. 使用python愉快地做高数线代题目~

    今天接触到了python,发现真是极易上手啊!对比c语言是什么鬼东西= = 诶,等下,看完教学文章发现TA在下面写了这句话 如果做了前面的内容你可能已被吸引了,觉得c语言真的是废材! 不...不是的. ...

  6. python 位运算与等号_Python 运算符

    和大多数语言一样,Python也有很多运算符,并且运算符跟其他语言的运算符大同小异接下来一一介绍: 算术运算符: 运算符描述实例 +加 - 两个对象相加a+b的输出结果是30 -减 - 得到复数或者一 ...

  7. python减小内存占用_如何将Python内存占用缩小20倍?

    当程序执行过程中RAM中有大量对象处于活动状态时,可能会出现内存问题,特别是在对可用内存总量有限制的情况下. 下面概述了一些减小对象大小的方法,这些方法可以显著减少纯Python程序所需的RAM数量. ...

  8. python中排序英文单词怎么写_Python实现对文件进行单词划分并去重排序操作示例...

    本文实例讲述了Python实现对文件进行单词划分并去重排序操作.,具体如下: 文件名:test1.txt 文件内容: But soft what light through yonder window ...

  9. python程序如何执行死刑图片_如何判断对象已死

    已死的对象就是不可能被任何途径使用的对象,有以下几种方法判断一个对象是否已经死了: 引用计数 给对象添加一个引用计数器,每当有一个地方引用他,计算器就加 1:当引用失效时,计数器减 1:任何时刻计数器 ...

  10. Python gRPC 安装

    1. 安装依赖库 sudo pip3 install grpcio sudo pip3 install protobuf sudo pip3 install grpcio_tools 2. 生成对应文 ...

最新文章

  1. docker 启动,关闭,查看运行状态
  2. 我是如何利用“王宝强离婚”事件来吸粉的
  3. linux suse 软件管理工具 zypper 简介
  4. Nova虚拟机启动提示libvirtError
  5. Java的GC机制及算法
  6. ubuntu anaconda配置环境变量_Anaconda从下载到环境变量配置(windows)
  7. 应用在tomcat下的四种部署方式(原创)
  8. 关于python全局变量
  9. [单片机框架][drivers层][ADC] fuelgauge 软件电量计(二)
  10. 如何设置matlab和.m、.slx .mdl .p .mat等文件之间关联
  11. 小米6通话音量补丁_手机通话声音小?只需打开这个开关,音量既大又清晰
  12. 安装pyltp遇到的问题
  13. yaml 变量引用_yaml语法
  14. Linux内存memtest,详细讲解 Linux极品内存检测软件 Memtest86
  15. 趣节点带您3分钟搞懂信息流广告
  16. heaps 和 priority queue堆和优先队列的定义和数据结构表示
  17. WEB客户端编程与服务器端编程
  18. 武汉理工大学数学建模大作业
  19. FreeModbus源码详解
  20. Excel公式——手机号区分运营商归属,电信,联通,移动手机号的区分

热门文章

  1. Domino XML Language(DXL)简介
  2. 基于python的-使用正则表达式验证手机号
  3. mysql中用来取余数的函数是_MySQL 常用函数总结
  4. 飞秋常见文件解决方案
  5. Ascii完整码表(256个)
  6. 俄罗斯方块C++代码(转载他人代码)
  7. 项目管理沙龙第二次聚会纪要
  8. 成语接龙、歇后语 js JavaScript html web nodejs成语接龙离线js库
  9. 关于SQL数据字典的详解
  10. 无需U盘在Windows下安装Linux系统实现双系统(非子系统)