python数学模块Word文档格式.docx
《python数学模块Word文档格式.docx》由会员分享,可在线阅读,更多相关《python数学模块Word文档格式.docx(7页珍藏版)》请在冰豆网上搜索。
isnotafloat,delegatesto
x.__ceil__(),whichshouldreturnan
Integral
value.
math.copysign(x,
y)
Returnafloatwiththemagnitude(absolutevalue)of
butthesignof
y.Onplatformsthatsupportsignedzeros,
copysign(1.0,
-0.0)
returns
-1.0.
math.fabs(x)
Returntheabsolutevalueof
x.
math.factorial(x)
Return
factorial.Raises
ValueError
if
isnotintegralorisnegative.
math.floor(x)
Returnthefloorof
x,thelargestintegerlessthanorequalto
isnotafloat,
delegatesto
x.__floor__(),whichshouldreturnan
math.fmod(x,
fmod(x,
y),asdefinedbytheplatformClibrary.NotethatthePythonexpression
%
y
maynotreturnthesameresult.TheintentoftheCstandardisthat
y)
beexactly(mathematically;
toinfiniteprecision)equalto
-
n*y
forsomeinteger
n
suchthattheresulthasthesamesignas
andmagnitudelessthan
abs(y).Python’s
yreturnsaresultwiththesignof
instead,andmaynotbeexactlycomputableforfloatarguments.Forexample,
fmod(-1e-100,
1e100)
is
-1e-100,buttheresultofPython’s
-1e-100
1e100
1e100-1e-100,whichcannotberepresentedexactlyasafloat,androundstothesurprising
1e100.Forthisreason,function
fmod()
isgenerallypreferredwhenworkingwithfloats,whilePython’s
ispreferredwhenworkingwithintegers.
math.frexp(x)
Returnthemantissaandexponentof
asthepair
(m,
e).
m
isafloatand
e
isanintegersuchthat
==
*
2**e
exactly.If
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
or
b
isnonzero,thenthevalueof
gcd(a,
b)
isthelargestpositiveintegerthatdividesboth
b.gcd(0,
0)
0.
Newinversion3.5.
math.isclose(a,
b,
*,
rel_tol=1e-09,
abs_tol=0.0)
True
ifthevalues
areclosetoeachotherand
False
otherwise.
Whetherornottwovaluesareconsideredcloseisdeterminedaccordingtogivenabsoluteandrelativetolerances.
rel_tol
istherelativetolerance–itisthemaximumalloweddifferencebetween
b,relativetothelargerabsolutevalueof
b.Forexample,tosetatoleranceof5%,pass
rel_tol=0.05.Thedefaulttoleranceis
1e-09,whichassuresthatthetwovaluesarethesamewithinabout9decimaldigits.
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
areonlyconsideredclosetothemselves.
Seealso:
PEP485
–Afunctionfortestingapproximateequality
math.isfinite(x)
isneitheraninfinitynoraNaN,and
otherwise.(Notethat
0.0
isconsideredfinite.)
Newinversion3.2.
math.isinf(x)
isapositiveornegativeinfinity,and
math.isnan(x)