当前位置: 首页 > 图灵资讯 > 行业资讯> Python之math模块常用方法汇总

Python之math模块常用方法汇总

发布时间:2025-10-26 16:07:51

整理python中math模块常用的方法

ceil:取大于或等于x的最小整数值,如果x是一个整数,则返回x

copysign:将y的正负号添加到x前,可以使用0

cos:要求x的余弦,x必须是弧度的

degrees:将x从弧度转换为角度

e:表示常量

exp:返回math.e,也就是2.71828x次方

expm1:返回math.e的x(其值为2.71828)次方值减1

fabs:返回x的绝对值

factorial:取X的阶乘值

floor:取小于等于x的整数值,如果x是一个整数,则返回自己

fmod:获得x/y的余数值是浮点数

frexp:返回一个元组(m,e),计算方法为:x分别除0.5和1,得到一个值的范围

fsum:求和操作迭代器中的每个元素

gcd:返回x和y的公约数

hypot:如果x是一个无限的数字,返回True,否则返回False

isfinite:如果x是正无穷大还是负无穷大,返回True,否则返回False

isinf:如果x是正无穷大还是负无穷大,返回True,否则返回False

isnan:如果x不是数字True,否则返回False

ldexp:返回x*(2**i)的值

log:返回x的自然对数,默认以e为基数,base参数给定时,x的对数返回给定的base,计算为:log(x)/log(base)

log10:以10为底返回x的对数

log1p:自然对数(基数为e)返回x+1的值

log2:基2对数返回x

modf:由x的小数部分和整数部分组成的元组返回

pi:数字常量,圆周率

pow:返回xy次方,即x**y

radians:将角度x转换为弧度

sin:x(x为弧度)的正弦值

sqrt:寻找x的平方根

tan:返回x(x为弧度)的正切值

trunc:返回x的整数部分

相关推荐:Python视频教程

ceil

#取大于或等于x的最小整数值,如果x是一个整数,则返回x
ceil(x)
Returntheceilingofxasanint.
Thisisthesmallestintegralvalue>=x.
>>>math.ceil(4.01)
5
>>>math.ceil(4.99)
5
>>>math.ceil(-3.99)
-3
>>>math.ceil(-3.01)
-3

copysign

#将y的正负号添加到x前,可以使用0
copysign(x,y)
Returnafloatwiththemagnitude(absolutevalue)ofxbutthesign
ofy.Onplatformsthatsupportsignedzeros,copysign(1.0,-0.0)
returns-1.0.
>>>math.copysign(2,3)
2.0
>>>math.copysign(2,-3)
-2.0
>>>math.copysign(3,8)
3.0
>>>math.copysign(3,-8)
-3.0

cos

#求x的余弦必须是弧度
cos(x)
Returnthecosineofx(measuredinradians).
#math.pi/4表示弧度,角度转换为45度
>>>math.cos(math.pi/4)
0.7071067811865476
math.pi/3表示弧度,角度转换为60度
>>>math.cos(math.pi/3)
0.5000000000000001
math.pi/6表示弧度,角度转换为30度
>>>math.cos(math.pi/6)
0.8660254037844387

degrees

#将x从弧度转换为角度
degrees(x)
Convertanglexfromradianstodegrees.
>>>math.degrees(math.pi/4)
45.0
>>>math.degrees(math.pi)
180.0
>>>math.degrees(math.pi/6)
29.999999999999996
>>>math.degrees(math.pi/3)
59.99999999999999

e

#表示常量
>>>math.e
2.718281828459045

exp

#返回math.e,也就是2.71828x次方
exp(x)
Returneraisedtothepowerofx.
>>>math.exp(1)
2.718281828459045
>>>math.exp(2)
7.38905609893065
>>>math.exp(3)
20.085536923187668

expm1

#返回math.e的x(其值为2.71828)次方值减1
expm1(x)
Returnexp(x)-1.
Thisfunctionavoidsthelossofprecisioninvolvedinthedirectevaluationofexp(x)-1forsmalx.
>>>math.expm1(1)
1.718281828459045
>>>math.expm1(2)
6.38905609893065
>>>math.expm1(3)
19.085536923187668

fabs

#返回x的绝对值
fabs(x)
Returntheabsolutevalueofthefloatx.
>>>math.fabs(-0.003)
0.003
>>>math.fabs(-110)
110.0
>>>math.fabs(100)
100.0

factorial

#取x的阶乘值
factorial(x)->Integral
Findx!.RaiseaValueErrorifxisnegativeornon-integral.
>>>math.factorial(1)
1
>>>math.factorial(2)
2
>>>math.factorial(3)
6
>>>math.factorial(5)
120
>>>math.factorial(10)
3628800

floor

#取一个小于或等于x的整数值,如果x是一个整数,则返回自己
floor(x)
Returnthefloorofxasanint.
Thisisthelargestintegralvalue<=x.
>>>math.floor(4.1)
4
>>>math.floor(4.999)
4
>>>math.floor(-4.999)
-5
>>>math.floor(-4.01)
-5

fmod

#获得x/y的余数值是浮点数
fmod(x,y)
Returnfmod(x,y),accordingtoplatformC.x%ymaydiffer.
>>>math.fmod(20,3)
2.0
>>>math.fmod(20,7)
6.0

frexp

#返回一个元组(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
frexp(x)
Returnthemantissaandexponentofx,aspair(m,e).
misafloatandeisanint,suchthatx=m*2.**e.
Ifxis0,mandeareboth0.Else0.5<=abs(m)<1.0.
>>>math.frexp(10)
(0.625,4)
>>>math.frexp(75)
(0.5859375,7)
>>>math.frexp(-40)
(-0.625,6)
>>>math.frexp(-100)
(-0.78125,7)
>>>math.frexp(100)
(0.78125,7)

fsum

#求和操作迭代器中的每个元素
fsum(iterable)
Returnanaccuratefloatingpointsumofvaluesintheiterable.
AssumesIEEE-754floatingpointarithmetic.
>>>math.fsum(1,2,3,4)
10.0
>>>math.fsum(1,2,3,4))
10.0
>>>math.fsum(-1,-2,-3,-4)
-10.0
>>>math.fsum([-1,-2,-3,-4]
-10.0

gcd

#返回x和y的公约数
gcd(x,y)->int
greatestcommonpisorofxandy
>>>math.gcd(8,6)
2
>>>math.gcd(40,20)
20
>>>math.gcd(8,12)
4

hypot

#得到(x**2+y**2),平方值
hypot(x,y)
ReturntheEuclideandistance,sqrt(x*x+y*y).
>>>math.hypot(3,4)
5.0
>>>math.hypot(6,8)
10.0

isfinite

#如果x是一个无限的数字,返回True,否则返回False
isfinite(x)->bool
ReturnTrueifxisneitheraninfinitynoraNaN,andFalseotherwise.
>>>math.isfinite(100)
True
>>>math.isfinite(0)
True
>>>math.isfinite(0.1)
True
>>>math.isfinite("a")
>>>math.isfinite(0.0001)
True

isinf

#如果x是正无穷大还是负无穷大,返回True,否则返回False
isinf(x)->bool
ReturnTrueifxisapositiveornegativeinfinity,andFalseotherwise.
>>>math.isinf(234)
False
>>>math.isinf(0.1)
False

isnan

#如果x不是数字True,否则返回False
isnan(x)->bool
ReturnTrueifxisaNaN(notanumber),andFalseotherwise.
>>>math.isnan(23)
False
>>>math.isnan(0.01)
False

ldexp

#返回x*(2**i)的值
ldexp(x,i)
Returnx*(2**i).
>>>math.ldexp(5,5)
160.0
>>>math.ldexp(3,5)
96.0

log

#返回x的自然对数,默认以e为基数,base参数给定时,x的对数返回给定的base,计算为:log(x)/log(base)
log(x[,base])
Returnthelogarithmofxtothegivenbase.
Ifthebasenotspecified,returnsthenaturallogarithm(basee)ofx.
>>>math.log(10)
2.302585092994046
>>>math.log(11)
2.3978952727983707
>>>math.log(20)
2.995732273553991

log10

#以10为底返回x的对数
log10(x)
Returnthebase10logarithofx.
>>>math.log10(10)
1.0
>>>math.log10(100)
2.0
#也就是说,10的1.3次方的结果是20
>>>math.log10(20)
1.3010299956639813

log1p

#自然对数(基数为e)返回x+1的值
log1p(x)
Returnthenaturllogarithmof1+x(basee).
Theresultiscomputedinawaywhichisaccurateforxnearzero.
>>>math.log(10)
2.302585092994046
>>>math.log1p(10)
2.3978952727983707
>>>math.log(11)
2.3978952727983707

log2

#基2对数返回x
log2(x)
Returnthebase2logarithmofx.
>>>math.log2(32)
5.0
>>>math.log2(20)
4.321928094887363
>>>math.log2(16)
4.0

modf

#返回由X的小数部分和整数部分组成的元组
modf(x)
Returnthefractionalandintegerpartsofx.Bothresultscarrythesign
ofxandarefloats.
>>>math.modf(math.pi)
(0.14159265358979312,3.0)
>>>math.modf(12.34)
(0.33999999999999986,12.0)

pi

#数字常量,圆周率
>>>print(math.pi)
3.141592653589793

pow

#返回xy次方,即x**y
pow(x,y)
Returnx**y(xtothepowerofy).
>>>math.pow(3,4)
81.0
>>>
>>>math.pow(2,7)
128.0

radians

#将角度x转换为弧度
radians(x)
Convertanglexfromdegreestoradians.
>>>math.radians(45)
0.7853981633974483
>>>math.radians(60)
1.0471975511965976

sin

#x(x为弧度)的正弦值
sin(x)
Returnthesineofx(measuredinradians).
>>>math.sin(math.pi/4)
0.7071067811865475
>>>math.sin(math.pi/2)
1.0
>>>math.sin(math.pi/3)
0.8660254037844386

sqrt

#寻找x的平方根
sqrt(x)
Returnthesquarerootofx.
>>>math.sqrt(100)
10.0
>>>math.sqrt(16)
4.0
>>>math.sqrt(20)
4.47213595499958

tan

#返回x(x为弧度)的正切值
tan(x)
Returnthetangentofx(measuredinradians).
>>>math.tan(math.pi/4)
0.9999999999999999
>>>math.tan(math.pi/6)
0.5773502691896257
>>>math.tan(math.pi/3)
1.7320508075688767

trunc

#返回x的整数部分
trunc(x:Real)->Integral
Truncatestothentertintenteraltord00.Usesthe__trunc__magicmethod.
>>>math.trunc(6.789)
6
>>>math.trunc(math.pi)
3
>>>math.trunc(2.567)
2

相关文章

Python之math模块常用方法汇总

Python之math模块常用方法汇总

2025-10-26
Python之json模块和pickle模块详解

Python之json模块和pickle模块详解

2025-10-26
python线程中的semaphore信号量是什么

python线程中的semaphore信号量是什么

2025-10-26
Python reversed函数及用法

Python reversed函数及用法

2025-10-26
Python截取字符串(字符串切片)方法详解

Python截取字符串(字符串切片)方法详解

2025-10-26
Python3 迭代器与生成器

Python3 迭代器与生成器

2025-10-24