python数学模块.docx
- 文档编号:3427919
- 上传时间:2022-11-23
- 格式:DOCX
- 页数:7
- 大小:18.79KB
python数学模块.docx
《python数学模块.docx》由会员分享,可在线阅读,更多相关《python数学模块.docx(7页珍藏版)》请在冰豆网上搜索。
python数学模块
9.2. math —Mathematicalfunctions¶
Thismoduleisalwaysavailable.ItprovidesaccesstothemathematicalfunctionsdefinedbytheCstandard.
Thesefunctionscannotbeusedwithcomplexnumbers;usethefunctionsofthesamenamefromthe cmath moduleifyourequiresupportforcomplexnumbers.Thedistinctionbetweenfunctionswhichsupportcomplexnumbersandthosewhichdon’tismadesincemostusersdonotwanttolearnquiteasmuchmathematicsasrequiredtounderstandcomplexnumbers.Receivinganexceptioninsteadofacomplexresultallowsearlierdetectionoftheunexpectedcomplexnumberusedasaparameter,sothattheprogrammercandeterminehowandwhyitwasgeneratedinthefirstplace.
Thefollowingfunctionsareprovidedbythismodule.Exceptwhenexplicitlynotedotherwise,allreturnvaluesarefloats.
9.2.1.Number-theoreticandrepresentationfunctions
math.ceil(x)
Returntheceilingof x,thesmallestintegergreaterthanorequalto x.If x isnotafloat,delegatesto x.__ceil__(),whichshouldreturnan Integral value.
math.copysign(x, y)
Returnafloatwiththemagnitude(absolutevalue)of x butthesignof y.Onplatformsthatsupportsignedzeros, copysign(1.0, -0.0) returns -1.0.
math.fabs(x)
Returntheabsolutevalueof x.
math.factorial(x)
Return x factorial.Raises ValueError if x isnotintegralorisnegative.
math.floor(x)
Returnthefloorof x,thelargestintegerlessthanorequalto x.If x isnotafloat,
delegatesto x.__floor__(),whichshouldreturnan Integral value.
math.fmod(x, y)
Return fmod(x, y),asdefinedbytheplatformClibrary.NotethatthePythonexpression x % y maynotreturnthesameresult.TheintentoftheCstandardisthat fmod(x, y) beexactly(mathematically;toinfiniteprecision)equalto x - n*y forsomeinteger n suchthattheresulthasthesamesignas x andmagnitudelessthan abs(y).Python’s x % yreturnsaresultwiththesignof y instead,andmaynotbeexactlycomputableforfloatarguments.Forexample, fmod(-1e-100, 1e100) is -1e-100,buttheresultofPython’s -1e-100 % 1e100 is 1e100-1e-100,whichcannotberepresentedexactlyasafloat,androundstothesurprising 1e100.Forthisreason,function fmod() isgenerallypreferredwhenworkingwithfloats,whilePython’s x % y ispreferredwhenworkingwithintegers.
math.frexp(x)
Returnthemantissaandexponentof x asthepair (m, e). m isafloatand e isanintegersuchthat x == m * 2**e exactly.If x iszero,returns (0.0, 0),otherwise 0.5 <= abs(m)< 1.Thisisusedto“pickapart”theinternalrepresentationofafloatinaportableway.
math.fsum(iterable)
Returnanaccuratefloatingpointsumofvaluesintheiterable.Avoidslossofprecisionbytrackingmultipleintermediatepartialsums:
>>>
>>>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
Thealgorithm’saccuracydependsonIEEE-754arithmeticguaranteesandthetypicalcasewheretheroundingmodeishalf-even.Onsomenon-Windowsbuilds,theunderlyingClibraryusesextendedprecisionadditionandmayoccasionallydouble-roundanintermediatesumcausingittobeoffinitsleastsignificantbit.
Forfurtherdiscussionandtwoalternativeapproaches,seethe ASPNcookbookrecipesforaccuratefloatingpointsummation.
math.gcd(a, b)
Returnthegreatestcommondivisoroftheintegers a and b.Ifeither a or b isnonzero,thenthevalueof gcd(a, b) isthelargestpositiveintegerthatdividesboth a and b.gcd(0, 0) returns 0.
Newinversion3.5.
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
Return True ifthevalues a and b areclosetoeachotherand False otherwise.
Whetherornottwovaluesareconsideredcloseisdeterminedaccordingtogivenabsoluteandrelativetolerances.
rel_tol istherelativetolerance–itisthemaximumalloweddifferencebetween a and b,relativetothelargerabsolutevalueof a or b.Forexample,tosetatoleranceof5%,pass rel_tol=0.05.Thedefaulttoleranceis 1e-09,whichassuresthatthetwovaluesarethesamewithinabout9decimaldigits. rel_tol mustbegreaterthanzero.
abs_tol istheminimumabsolutetolerance–usefulforcomparisonsnearzero. abs_tolmustbeatleastzero.
Ifnoerrorsoccur,theresultwillbe:
abs(a-b) <= max(rel_tol * max(abs(a), abs(b)),abs_tol).
TheIEEE754specialvaluesof NaN, inf,and -inf willbehandledaccordingtoIEEErules.Specifically, NaN isnotconsideredclosetoanyothervalue,including NaN. inf and -inf areonlyconsideredclosetothemselves.
Newinversion3.5.
Seealso:
PEP485 –Afunctionfortestingapproximateequality
math.isfinite(x)
Return True if x isneitheraninfinitynoraNaN,and False otherwise.(Notethat 0.0 isconsideredfinite.)
Newinversion3.2.
math.isinf(x)
Return True if x isapositiveornegativeinfinity,and False otherwise.
math.isnan(x)
Return True
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- python 数学 模块