Python模块(包)
Python模块(包)-math模块
Python模块(包)-math模块
关于math模块
math模块中定义了一些数学函数。由于这个模块属于编译系统自带,因此它可以被无条件调用。该模块还提供了与用标准C定义的数学函数的接口。
要注意的是,这些函数无法应用于复数,但是在cmath模块中定义的同名函数可以支持复数运算,当然也会为此付出一定的性能代价。为什么会设置同名的函数却会有是否支持复数的区别,这是因为大多数用户完全不需要复数方面的操作,因此对于他们来讲,理解函数就会有不必要的麻烦。接受一个例外而不是一个复数结果使得参数被设置为复数这个意外更加容易被察觉,那么编程者就可以再次确认以下为什么要定义并在这里产生它。
以下的函数是该模块所提供的。除非明确指出,否则所有返回值都是浮点数。
可以通过dir(math)查看math对象的全部属性和方法列表。
math.pi
数字常量,圆周率,取到小数点后15位
代码实例:
import math print(type(math.pi)) # <class 'float'> print(math.pi) # 3.141592653589793
math.ceil(x)
返回x的最大值,这个最大值是大于或等于x的最小整数。如果x不是浮点数的话,将使用x.__ceil__()替代返回一个整数值。
代码实例:
import math print(type(math.ceil(2.22))) # <class 'int'> print(math.ceil(2.22)) # 3 print(math.ceil(2)) # 2 print(math.ceil(-5)) # -5 print(math.ceil(-5.66)) # -5
math.copysign(x,y)
把y的正负号加到x前面,可以使用0,返回一个浮点数。
代码实例:
import math print(type(math.copysign(2,3))) # <class 'float'> print(math.copysign(2,3)) # 2.0 print(math.copysign(2,-3)) # -2.0 print(math.copysign(-2,-3)) # -2.0 print(math.copysign(2.23,-3.65)) # -2.23 print(math.copysign(-2.23,3.65)) # 2.23 print(math.copysign(-2.23,-3.65)) # -2.23
math.cos(x)
求x的余弦,x必须是弧度,返回一个余弦值(浮点数)
代码实例:
import math #math.pi/4表示弧度,转换成角度为45度 print(type(math.cos(math.pi/4))) # <class 'float'> print(math.cos(math.pi/4)) # 0.7071067811865476 # math.pi/3表示弧度,转换成角度为60度 print(math.cos(math.pi/3)) # 0.5000000000000001 # math.pi/6表示弧度,转换成角度为30度 print(math.cos(math.pi/6)) # 0.8660254037844387
math.sin(x)
求x的正弦,x必须是弧度,返回一个余弦值(浮点数)
代码实例:
import math #math.pi/4表示弧度,转换成角度为45度 print(type(math.sin(math.pi/4))) # <class 'float'> print(math.sin(math.pi/4)) # 0.7071067811865475 # math.pi/3表示弧度,转换成角度为60度 print(math.sin(math.pi/3)) # 0.8660254037844386 # math.pi/6表示弧度,转换成角度为30度 print(math.sin(math.pi/6)) # 0.49999999999999994 # math.pi/2表示弧度,转换成角度是90度 print(math.sin(math.pi/2)) # 1.0 print(math.sin(0)) # 0.0
math.degrees(x)
把x从弧度转换成角度,返回一个浮点数的角度值
代码实例:
import math print(type(math.degrees(math.pi/4))) # <class 'float'> print(math.degrees(math.pi/6)) # 29.999999999999996 print(math.degrees(math.pi/4)) # 45.0 print(math.degrees(math.pi/3)) # 59.99999999999999 print(math.degrees(math.pi/2)) # 90.0 print(math.degrees(math.pi)) # 180.0
math.radians(x)
把角度x转换成弧度
代码实例:
import math print(type(math.radians(45))) # <class 'float'> print(math.radians(0)) # 0.0 print(math.radians(45)) # 0.7853981633974483 print(math.radians(30)) # 0.5235987755982988 print(math.radians(60)) # 1.0471975511965976 print(math.radians(90)) # 1.5707963267948966 print(math.radians(180)) # 3.141592653589793
math.e
表示一个常量,当x趋近于正无穷或负无穷时,[1+(1/x)]^x的极限就等于e,实际上e就是通过这个极限而发现的。它是个无限不循环小数。而且e还是一个超越数
代码实例:
import math print(type(math.e)) # <class 'float'> print(math.e) # 2.718281828459045
math.exp(x)
返回math.e,也就是2.71828的x次方
代码实例:
import math print(type(math.exp(2))) # <class 'float'> print(math.exp(1)) # 2.718281828459045 print(math.exp(2)) # 7.38905609893065 print(math.exp(10)) # 22026.465794806718 print(math.exp(-10)) # 4.5399929762484854e-05
math.expm1(x)
返回math.e的x(其值为2.71828)次方的值减1
代码实例:
import math print(type(math.expm1(2))) # <class 'float'> print(math.expm1(1)) #1.718281828459045 print(math.expm1(2)) # 6.38905609893065 print(math.expm1(10)) # 22025.465794806718 print(math.expm1(-10)) # -0.9999546000702375
math.fabs(x)
返回x的绝对值,浮点数
代码实例:
import math print(type(math.fabs(2.45))) # <class 'float'> print(math.fabs(2.45)) #2.45 print(math.fabs(-2.45)) # 2.45 print(math.fabs(10)) # 10.0 print(math.fabs(-10)) # 10.0
内置函数abs()和math.fabs()区别
1,abs()是一个内置函数,而fabs()在math模块中定义的。
2,fabs()函数只适用于float和integer类型,而abs()也适用于复数。
3,abs()返回是float和int类型,math.fabs()返回是float类型
math.factorial(x)
取x的阶乘的值,返回一个整数
参数x必须为一个正整数
代码实例:
import math # 阶乘 x*(x-1)*(x-2)...2*1 print(type(math.factorial(2))) # <class 'int'> print(math.factorial(2)) # 2 print(math.factorial(3)) # 6 print(math.factorial(10)) # 3628800
math.floor(x)
取小于等于x的最大的整数值,如果x是一个整数,则返回自身
返回一个整数值
代码实例:
import math print(type(math.floor(2.33))) # <class 'int'> print(math.floor(3)) # 3 print(math.floor(-3)) # -3 print(math.floor(3.12)) # 3 print(math.floor(3.98)) # 3 print(math.floor(-3.12)) # -4 print(math.floor(-3.98)) # -4
math.fmod(x,y)
得到x/y的余数,其值是一个浮点数
代码实例:
import math print(type(math.fmod(20,6))) # <class 'float'> print(math.fmod(20,6)) # 2.0 print(math.fmod(20,-6)) # 2.0 print(math.fmod(-20,6)) # -2.0 print(math.fmod(80.44,4)) # 0.4399999999999977 print(math.fmod(-80,4.55)) # -2.650000000000003
math.fmod(x,y) 和 x % y的区别:
1,前者始终返回浮点数;后者在x、y均为整型时,返回整型,其他情况下,即x、y任有一浮点型时,结果均返回浮点型。
2,前者返回结果的符号(正负)始终与x相同;后者所得结果的符号(正负)始终与y相同。
3,前者返回结果的绝对值始终等于“|x| % |y|”;对于后者,若x与y符号(正负)相同,结果的绝对值为“|x| % |y|”,若x与y的符号(正负)不同,结果的绝对值为“|y| - (|x| % |y|)”。
math.frexp(x)
返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
代码实例:
import math print(type(math.frexp(10))) # <class 'tuple'> print(math.frexp(10)) # (0.625, 4) print(math.frexp(75)) # (0.5859375, 7) print(math.frexp(-40)) # (-0.625, 6) print(math.frexp(-100)) # (-0.78125, 7) print(math.frexp(100)) # (0.78125, 7)
math.fsum(iterable)
对迭代器里的每个元素进行求和操作,返回一个浮点数
代码实例:
import math print(type(math.fsum([1,2,3,4]))) # <class 'float'> print(math.fsum([1,2,3,4])) # 10.0 print(math.fsum([-1,-2,-3,-4])) # -10.0 print(math.fsum([-1,-3,5,7])) # 8.0 print(math.fsum([-14,3,-55,7])) # -59.0 print(math.fsum([-14.77,3.34,-5.5,7])) # -9.93
math.gcd(x,y)
x,y必须为整数,返回x和y的最大公约数(整数)。
代码实例:
import math print(math.gcd(4,8)) # 4 print(math.gcd(-4,8)) # 4 print(math.gcd(4,-8)) # 4 print(math.gcd(12,13)) # 1 print(math.gcd(234,435)) # 3
math.hypot(x,y)
得到(x**2+y**2)再求平方根的值。返回一个正浮点数
代码实例:
import math print(math.hypot(3,4)) # 5.0 print(math.hypot(3,-4)) # 5.0 print(math.hypot(-3,4)) # 5.0 print(math.hypot(-3,-4)) # 5.0 print(math.hypot(3.54,4)) # 5.341497917251302 print(math.hypot(3.54,4.34)) # 5.600642820248404 print(math.hypot(-3.54,4.34)) # 5.600642820248404 print(math.hypot(3.54,-4.34)) # 5.600642820248404
math.isfinite(x)
判断x是不是为一个无穷数,如果不是返回True,如果是返回False
代码实例:
import math print(type(math.isfinite(3))) # <class 'bool'> print(math.isfinite(3)) # True print(math.isfinite(3.44)) # True print(math.isfinite(-3.44)) # True print(math.isfinite(10/3)) # True
math.isinf(x)
如果x是正无穷大或负无穷大,则返回True,否则返回False
代码实例:
import math print(type(math.isinf(3))) # <class 'bool'> print(math.isinf(3)) # False print(math.isinf(3.44)) # False print(math.isinf(-3.44)) # False print(math.isinf(10/3)) # False
math.isnan(x)
如果x不是数字True,否则返回False
代码实例:
import math print(type(math.isnan(3))) # <class 'bool'> print(math.isnan(3)) # False print(math.isnan(3.44)) # False print(math.isnan(-3.44)) # False
math.ldexp(x,i)
返回x*(2**i)的值
代码实例:
import math print(type(math.ldexp(3,3))) # <class 'float'> print(math.ldexp(3,3)) # 24.0 print(math.ldexp(5,4)) # 80.0 print(math.ldexp(5,14)) # 81920.0 print(math.ldexp(10,2)) # 40.0
math.modf(x)
返回由x的小数部分和整数部分组成的元组
代码实例:
import math print(type(math.modf(3.66))) # <class 'float'> print(math.modf(3.66)) # (0.6600000000000001, 3.0) print(math.modf(-3.66)) # (-0.6600000000000001, -3.0) print(math.modf(3)) # (0.0, 3.0) print(math.modf(-3)) # (-0.0, -3.0) print(math.modf(-3)[0]) # -0.0 print(math.modf(-3)[1]) # -3.0
math.pow(x,y)
返回x的y次方,即x**y
代码实例:
import math print(type(math.pow(3,3))) # <class 'float'> print(math.pow(3,3)) # 27.0 print(math.pow(5,-4)) # 0.0016 print(math.pow(-5,2)) # 25.0 print(math.pow(-5,3)) # -125.0 print(math.pow(10.55,2)) #111.30250000000001
math.sqrt(x)
求x的平方根
代码实例:
import math print(type(math.sqrt(45))) # <class 'float'> print(math.sqrt(0)) # 0.0 print(math.sqrt(4)) # 0.0 print(math.sqrt(4.88)) # 2.209072203437452 print(math.sqrt(-4)) # ValueError: math domain error
math.tan(x)
返回x(x为弧度)的正切值
代码实例:
import math print(type(math.tan(45))) # <class 'float'> print(math.tan(0)) # 0.0 print(math.tan(math.pi/3)) # 1.7320508075688767 print(math.tan(45)) # 1.6197751905438615 print(math.tan(48.88)) # -5.334337001200935 print(math.tan(60)) # 0.320040389379563 print(math.tan(-60)) # -0.320040389379563
math.trunc(x)
返回x的整数部分
代码实例:
import math print(type(math.trunc(45.67))) # <class 'int'> print(math.trunc(45.67)) # 45 print(math.trunc(-45.67)) # -45 print(math.trunc(math.pi)) # 3 print(math.trunc(math.e)) # 2
最后修改:2020年3月27日 01:07