python-math-基础使用

python手册9.2
http://docs.python.org/2.7/library/math.html

9.2. math — (数学方法)Mathematical functions

这个模块采用标准C实现的。

这个方法不能用于复数;如果要用在复数里就用cmath,这两个方法的区别是支持复数和很多用户不想知道复数的东西。返回一个异常来取代复数结果,这样更有利早点处理异常。

下面的方法是这个模块提供的。除非明确说明,否则返回float。

9.2.1. (数字和代数)Number-theoretic and representation functions

math.ceil(x)
结果是不小于x的最小整数  math.ceil(20)  -- 20.0   math.ceil(-20.3) --  -20.0

math.copysign(x, y)
将y的“正负号” 给x 返回,并且支持0 ,copysign(1.0, -0.0) returns -1.0.

math.fabs(x)
返回 x的绝对值 |x|.

math.factorial(x)
返回 x的阶乘 x!,如果x不是整数或x是负数抛出异常。

math.floor(x)
返回小于等于x的最大整数 math.floor(-22.2) --  -23.0

math.fmod(x, y)
返回 x%y . 由C库实现。注意可能和 x % y 返回结果不一样。标准C是使用 fmod(x,y)更精确= x-n*y 结果有x同样的符号,大小小于abs(y).x%y  返回y的符号,不能明确的计算出float参数。所以方法fmod()用于float,x%y 用于int型。

math.frexp(x)
返回一个二元组,分别是x的指数部分与尾数部分 (m, e)  , x == m * 2**e ,math.frexp(123) -- (0.9609375, 7)    0.9609375*(2**7) = 123.0

math.fsum(iterable)
计算一个可迭代的值的总和,避免丢失精度:
>>> 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
(以后再了解为什么)

math.isinf(x)
x 是不是正负无穷大.

math.isnan(x)
x是不是NaN(不是一个数字),

math.ldexp(x, i)
Return x * (2**i). 和frexp()相反.

math.modf(x)
返回x的小数部分和整数部分。

math.trunc(x)
Return x的整数部分(truncated 截断)int(x)

9.2.2. (对数和方乘)Power and logarithmic functions

math.exp(x)

    Return e**x.

math.expm1(x)

    Return e**x - 1. x非常小的时候使用,会更精确。

math.log(x[, base])

    log x以b为底,b可选,默认为 e  

math.log1p(x)

    log(1+x)以e为底,x近似于0时更准确     

    New in version 2.6.

math.log10(x)

    log(x, 10)

math.pow(x, y)

    x**y

math.sqrt(x)

    Return 开根号x.

9.2.3. (三角方法)Trigonometric functions

math.acos(x)

    Return the arc cosine of x, in 弧度.

math.asin(x)

    Return the arc sine of x, in 弧度.

math.atan(x)

    Return the arc tangent of x, in 弧度.

math.atan2(y, x)

    返回弧度y / x的反正切  

math.cos(x)

    Return the cosine of x 角度.

math.hypot(x, y)

    返回欧氏距离, sqrt(x*x + y*y).

math.sin(x)

    Return the sine of x 角度.

math.tan(x)

    Return the tangent of x 角度.

9.2.4. (角度转换)Angular conversion

math.degrees(x)

    将 x 从弧度变为角度

math.radians(x)

    将 x 从角度变为弧度

9.2.5. (双曲线方法)Hyperbolic functions

math.acosh(x)

    Return the inverse hyperbolic cosine of x.

math.asinh(x)

    Return the inverse hyperbolic sine of x.

math.atanh(x)

    Return the inverse hyperbolic tangent of x.

math.cosh(x)

    Return the hyperbolic cosine of x.

math.sinh(x)

    Return the hyperbolic sine of x.

math.tanh(x)

    Return the hyperbolic tangent of x.

9.2.6. (特殊方法)Special functions

math.erf(x)

    Return  x的误差函数.

math.erfc(x)

    Return x 的互补误差函数.

math.gamma(x)

    Return the Gamma function at x.

math.lgamma(x)

    Return the natural logarithm of the absolute value of the Gamma function at x.

9.2.7. (常数)Constants

math.pi

    π = 3.141592...

math.e

    e = 2.718281...

posted on 2012-12-03 19:14  DON'T PANIC  阅读(2180)  评论(0编辑  收藏  举报

导航