python数学模块

上传人:第*** 文档编号:61701613 上传时间:2018-12-10 格式:DOCX 页数:6 大小:21.92KB
返回 下载 相关 举报
python数学模块_第1页
第1页 / 共6页
python数学模块_第2页
第2页 / 共6页
python数学模块_第3页
第3页 / 共6页
python数学模块_第4页
第4页 / 共6页
python数学模块_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《python数学模块》由会员分享,可在线阅读,更多相关《python数学模块(6页珍藏版)》请在金锄头文库上搜索。

1、9.2.math Mathematical functionsThis module is always available. It provides access to the mathematical functions defined by the C standard.These functions cannot be used with complex numbers; use the functions of the same name from thecmathmodule if you require support for complex numbers. The disti

2、nction between functions which support complex numbers and those which dont is made since most users do not want to learn quite as much mathematics as required to understand complex numbers. Receiving an exception instead of a complex result allows earlier detection of the unexpected complex number

3、used as a parameter, so that the programmer can determine how and why it was generated in the first place.The following functions are provided by this module. Except when explicitly noted otherwise, all return values are floats.9.2.1. Number-theoretic and representation functionsmath.ceil(x)Return t

4、he ceiling ofx, the smallest integer greater than or equal tox. Ifxis not a float, delegates tox._ceil_(), which should return anIntegralvalue.math.copysign(x,y)Return a float with the magnitude (absolute value) ofxbut the sign ofy. On platforms that support signed zeros,copysign(1.0,-0.0)returns-1.

5、0.math.fabs(x)Return the absolute value ofx.math.factorial(x)Returnxfactorial. RaisesValueErrorifxis not integral or is negative.math.floor(x)Return the floor ofx, the largest integer less than or equal tox. Ifxis not a float, delegates tox._floor_(), which should return anIntegralvalue.math.fmod(x,

6、y)Returnfmod(x,y), as defined by the platform C library. Note that the Python expressionx%ymay not return the same result. The intent of the C standard is thatfmod(x,y)be exactly (mathematically; to infinite precision) equal tox-n*yfor some integernsuch that the result has the same sign asxand magni

7、tude less thanabs(y). Pythonsx%yreturns a result with the sign ofyinstead, and may not be exactly computable for float arguments. For example,fmod(-1e-100,1e100)is-1e-100, but the result of Pythons-1e-100%1e100is1e100-1e-100, which cannot be represented exactly as a float, and rounds to the surprisi

8、ng1e100. For this reason, functionfmod()is generally preferred when working with floats, while Pythonsx%yis preferred when working with integers.math.frexp(x)Return the mantissa and exponent ofxas the pair(m,e).mis a float andeis an integer such thatx=m*2*eexactly. Ifxis zero, returns(0.0,0), otherw

9、ise0.5=abs(m) 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.0The algorithms accuracy depends on IEEE-754 arithmetic guarantees and the typical case where the rounding mode is half-even. On some non-Windows builds, the underlying C library

10、 uses extended precision addition and may occasionally double-round an intermediate sum causing it to be off in its least significant bit.For further discussion and two alternative approaches, see theASPN cookbook recipes for accurate floating point summation.math.gcd(a,b)Return the greatest common

11、divisor of the integersaandb. If eitheraorbis nonzero, then the value ofgcd(a,b)is the largest positive integer that divides bothaandb.gcd(0,0)returns0.New in version 3.5.math.isclose(a,b,*,rel_tol=1e-09,abs_tol=0.0)ReturnTrueif the valuesaandbare close to each other andFalseotherwise.Whether or not

12、 two values are considered close is determined according to given absolute and relative tolerances.rel_tolis the relative tolerance it is the maximum allowed difference betweenaandb, relative to the larger absolute value ofaorb. For example, to set a tolerance of 5%, passrel_tol=0.05. The default to

13、lerance is1e-09, which assures that the two values are the same within about 9 decimal digits.rel_tolmust be greater than zero.abs_tolis the minimum absolute tolerance useful for comparisons near zero.abs_tolmust be at least zero.If no errors occur, the result will be:abs(a-b)=max(rel_tol*max(abs(a)

14、,abs(b),abs_tol).The IEEE 754 special values ofNaN,inf, and-infwill be handled according to IEEE rules. Specifically,NaNis not considered close to any other value, includingNaN.infand-infare only considered close to themselves.New in version 3.5.See also:PEP 485 A function for testing approximate equalitymath.isfinite(x)ReturnTrueifxis neither an infinity nor a NaN, andFalseotherwise. (Note that0.0isconsidered finite.)New in version 3.2.math.isinf(x)ReturnTrueifxis a positive or negative infinity, andFalseotherwise.math.isnan(x)ReturnTrueifx

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号