python 数学函数(翻译整理)

danny posted @ 2014年3月22日 15:00 in python with tags python math , 3162 阅读

翻译自: http://docs.python.org/3/library/math.html

实用至上,所以我并不会把他们全部内容都翻译出来.

数论及表示函数

math.ceil(x)

返回大于等于x的最小的整数 如果x不是小数,则返回自身.

>>> ceil(10)
10
>>> ceil(10.1)
11
>>> ceil(-10.1)
-10

math.copysign(xy)

返回x的绝对值乘以加上y的符号(正负) 即|x|*y的符号

>>> copysign(10, +0)
10.0
>>> copysign(10, -0.0)
-10.0
>>> copysign(10, -1.0)
-10.0

math.fabs(x)

返回x的绝对值

math.factorial(x)

返回x的阶乘, 如果x不是整数或者x是负数,将会引起"值错误"的异常

math.floor(x)

返回小于或者等于x的最大的整数,如果x不是小数,等于其自身 地板呃

 

>>> math.floor(10.1)
10
>>> math.floor(10.9)
10

math.fmod(xy)

???

math.frexp(x)

???

math.fsum(iterable)

返回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.isfinite(x)

返回真,如果x既不是无穷大也不是 NaN(非数字, not a number, 比如0/0), 否则返回假. 注意0.0被认为是有限的

New in version 3.2.

math.isinf(x)

返回真,如果x是正无穷或者负无穷,否则返回假

math.isnan(x)

返回真,如果x是NaN(非数字, not a number), 否则返回假

math.ldexp(xi)

返回x * (2**i), 这完全是跟 frexp()  反的.

math.modf(x)

???

math.trunc(x)

???

指数和对数函数

math.exp(x)
返回e**x, 即e的x次方
math.expm1(x)
返回e**x-1,并保证精度, exp(x)-1可能会损失精度  
New in version 3.2

math.log(x[base])

返回以e为底x的对数,如果不指定底数base

math.log1p(x)

返回以e为底1+x的对数,当x接近0时,很管用.

math.log2(x)

返回以2为底x的对数,比 log(x, 2)更精确 

New in version 3.3

math.pow(xy)

返回x**y,即x的y次方

不过,不像内置操作符**, math.pow() 会把参数都转换成小数.请使用**或者pow来计算的整数的准确乘方.

math.sqrt(x)

返回x的平方根(正值部分)

三角函数

凡是a开头的都是反三角函数

此处的函数多数返回弧度,除了部分标记的.

math.acos(x)

返回cos(x)的反三角函数的值

math.asin(x)

math.atan(x)

math.atan2(yx)

返回atan(y/x), 结果介于-pi和pi. 从原点到点(x,y)的向量与x轴正方向的角度.

...

math.cos(x)

math.hypot(xy)

返回欧几里德范数 sqrt(x*x + y*y). 这是从原点到点(x,y)的距离

math.sin(x)

math.tan(x)

角度转换

math.degrees(x)

弧度转角度

math.radians(x)

角度转弧度

Hyperbolic functions

???

Special functions

???

常量

math.pi

π = 3.141592...

math.e

e = 2.718281...,


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter