python数学模块.docx

上传人:b****5 文档编号:3427919 上传时间:2022-11-23 格式:DOCX 页数:7 大小:18.79KB
下载 相关 举报
python数学模块.docx_第1页
第1页 / 共7页
python数学模块.docx_第2页
第2页 / 共7页
python数学模块.docx_第3页
第3页 / 共7页
python数学模块.docx_第4页
第4页 / 共7页
python数学模块.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

python数学模块.docx

《python数学模块.docx》由会员分享,可在线阅读,更多相关《python数学模块.docx(7页珍藏版)》请在冰豆网上搜索。

python数学模块.docx

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 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1