您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > python库-math
importmathdir(math)Out[2]:['__doc__','__name__','__package__','acos',---反余弦'acosh',---反双曲余弦'asin',---反正弦'asinh',---反双曲正弦'atan',---反正切'atan2',---math.atan(y,x),y/x的反正切'atanh',---反双曲正切'ceil',---返回=x的最小整数,反floor'copysign',---math.copysign(x,y),返回与y同号的x值(改变x值符号)'cos',---余弦'cosh',---反余弦'degrees',---将弧长转化为角度,反radians'e',---2。7128'erf',---误差函数'erfc',---1-erf'exp',---e的x次方'expm1',---e的x次方-1'fabs',---绝对值'factorial',---x阶乘'floor',---返回=x的最大整数,反ceil'fmod',---返回x对y取模的余数,以x来决定余数的符号,%以y来决定余数的符号'frexp',---反ldexp,frexp(x),返回m,n'fsum',---返回x阵列各项和,例math.frexp([2,5])'gamma',---伽马函数'hypot',---hypot(x,y)平方和开根号'isinf',---是否为正负无穷大,是则true'isnan',---x=NaN,true,x为0也返回false'ldexp',---ldexp(x,y),输出m*2的n次方,反frexp'lgamma',---伽马函数的自然对数'log',---log(x,a),a不写则为e'log10',---log10(x)'log1p',---返回'modf',---返回x的小数部份与整数部份,似trunc'pi',---π'pow',---返回xy'radians',---將x(角度)转成弧长,与degrees为反函数'sin',---正弦'sinh',---反正弦'sqrt',---开根号'tan',---正切'tanh',---反正切'trunc']---返回x的整数部份,等同int,似modf两个常量pi,e举例:math.fmod(5,3)Out[12]:2.0In[14]:math.fmod(-5,3)Out[14]:-2.0In[15]:math.fmod(5,-3)Out[15]:2.0In[16]:math.fmod(-5,-3)Out[16]:-2.05%3Out[17]:25%(-3)Out[19]:-1-5%3Out[20]:1-5%(-3)Out[21]:-2官方文档:9.2.math—MathematicalfunctionsThismoduleisalwaysavailable.ItprovidesaccesstothemathematicalfunctionsdefinedbytheCstandard.Thesefunctionscannotbeusedwithcomplexnumbers;usethefunctionsofthesamenamefromthecmathmoduleifyourequiresupportforcomplexnumbers.Thedistinctionbetweenfunctionswhichsupportcomplexnumbersandthosewhichdon’tismadesincemostusersdonotwanttolearnquiteasmuchmathematicsasrequiredtounderstandcomplexnumbers.Receivinganexceptioninsteadofacomplexresultallowsearlierdetectionoftheunexpectedcomplexnumberusedasaparameter,sothattheprogrammercandeterminehowandwhyitwasgeneratedinthefirstplace.Thefollowingfunctionsareprovidedbythismodule.Exceptwhenexplicitlynotedotherwise,allreturnvaluesarefloats.9.2.1.Number-theoreticandrepresentationfunctionsmath.ceil(x)Returntheceilingofxasafloat,thesmallestintegervaluegreaterthanorequaltox.math.copysign(x,y)Returnxwiththesignofy.Onaplatformthatsupportssignedzeros,copysign(1.0,-0.0)returns-1.0.Newinversion2.6.math.fabs(x)Returntheabsolutevalueofx.math.factorial(x)Returnxfactorial.RaisesValueErrorifxisnotintegralorisnegative.Newinversion2.6.math.floor(x)Returnthefloorofxasafloat,thelargestintegervaluelessthanorequaltox.math.fmod(x,y)Returnfmod(x,y),asdefinedbytheplatformClibrary.NotethatthePythonexpressionx%ymaynotreturnthesameresult.TheintentoftheCstandardisthatfmod(x,y)beexactly(mathematically;toinfiniteprecision)equaltox-n*yforsomeintegernsuchthattheresulthasthesamesignasxandmagnitudelessthanabs(y).Python’sx%yreturnsaresultwiththesignofyinstead,andmaynotbeexactlycomputableforfloatarguments.Forexample,fmod(-1e-100,1e100)is-1e-100,buttheresultofPython’s-1e-100%1e100is1e100-1e-100,whichcannotberepresentedexactlyasafloat,androundstothesurprising1e100.Forthisreason,functionfmod()isgenerallypreferredwhenworkingwithfloats,whilePython’sx%yispreferredwhenworkingwithintegers.math.frexp(x)Returnthemantissaandexponentofxasthepair(m,e).misafloatandeisanintegersuchthatx==m*2**eexactly.Ifxiszero,returns(0.0,0),otherwise0.5=abs(m)1.Thisisusedto“pickapart”theinternalrepresentationofafloatinaportableway.math.fsum(iterable)Returnanaccuratefloatingpointsumofvaluesintheiterable.Avoidslossofprecisionbytrackingmultipleintermediatepartialsums:sum([.1,.1,.1,.1,.1,.1,.1,.1,.1,.1])0.9999999999999999fsum([.1,.1,.1,.1,.1,.1,.1,.1,.1,.1])1.0Thealgorithm’saccuracydependsonIEEE-754arithmeticguaranteesandthetypicalcasewheretheroundingmodeishalf-even.Onsomenon-Windowsbuilds,theunderlyingClibraryusesextendedprecisionadditionandmayoccasionallydouble-roundanintermediatesumcausingittobeoffinitsleastsignificantbit.Forfurtherdiscussionandtwoalternativeapproaches,seetheASPNcookbookrecipesforaccuratefloatingpointsummation.Newinversion2.6.math.isinf(x)Checkifthefloatxispositiveornegativeinfinity.Newinversion2.6.math.isnan(x)CheckifthefloatxisaNaN(notanumber).FormoreinformationonNaNs,seetheIEEE754standards.Newinversion2.6.math.ldexp(x,i)Returnx*(2**i).Thisisessentiallytheinverseoffunctionfrexp().math.modf(x)Returnthefractionalandintegerpartsofx.Bothresultscarrythesignofxandarefloats.math.trunc(x)ReturntheRealvaluextruncatedtoanIntegral(usuallyalonginteger).Usesthe__trunc__method.Newinversion2.6.Notethatfrexp()andmodf()haveadifferentcall/returnpatternthantheirCequivalents:theytakeasingleargumentandreturnapairofvalues,ratherthanreturningtheirsecondreturnvaluethroughan‘outputparameter’(thereisnosuchthinginPython).Fortheceil(),floor(),andmodf()functions,notethatallfloating-pointnumbersofsufficientlylargemagnitudeareexactintegers.Pythonfloatstypicallycarrynomorethan53bitsofprecision(thesameastheplatformCdoubletype),inwhichcaseanyfloatxwithabs(x)=2**52necessarilyhasnofractionalbits.9.2.2.Powerandlogarithmicfunctionsmath.exp(x)Returne**x.math.expm1(x)Returne**x-1.Forsmallfloatsx,thesubtractioninexp(x)-1canresultinasignificantlossofprecision;theexpm1()functionprovidesawaytocomput
本文标题:python库-math
链接地址:https://www.777doc.com/doc-4210728 .html