## 2012年4月11日 星期三

### [Python Std Library] Numeric and Math Modules : math — Mathematical functions

Preface :

Number-theoretic and representation functions :

math.ceil(x)
Return the ceiling of x as a float, the smallest integer value greater than or equal to x.
>>> math.ceil(1.2)
2
>>> math.ceil(1.0)
1
>>> math.ceil(-1.2)
-1 # Not -2

math.copysign(xy)
New in version 2.6.
Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0.

math.fabs(x)
Return the absolute value of x.
>>> math.fabs(-1.1)
1.1
>>> math.fabs(-1)
1.0 # 整數會轉浮點數

math.factorial(x)
New in version 2.6.
Return x factorial. Raises ValueError if x is not integral or is negative.
>>> math.factorial(5)
120 # 5! = 120

math.floor(x)
Return the floor of x as a float, the largest integer value less than or equal to x.
>>> math.floor(1.2)
1
>>> math.floor(-1.2)
-2

math.fmod(xy)
function fmod() is generally preferred when working with floats, while Python’s x % y is preferred when working with integers.

math.frexp(x)
Return the mantissa and exponent of x as the pair (m, e)m is a float and e is an integer such that x == m * 2**e exactly. If x is zero, returns (0.0, 0), otherwise0.5 <= abs(m) < 1. This is used to "pick apart" the internal representation of a float in a portable way.
>>> math.frexp(3)
(0.75, 2) # 3 = 0.75 * 2**2

math.fsum(iterable)
New in version 2.6.
Return an accurate floating point sum of values in the iterable. Avoids loss of precision by tracking multiple intermediate partial sums :
>>> math.fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0
>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.9999999999999999

The algorithm’s 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 uses extended precision addition and may occasionally double-round an intermediate sum causing it to be off in its least significant bit.

math.isinf(x)
New in version 2.6.
Check if the float x is positive or negative infinity.

math.isnan(x)
New in version 2.6.
Check if the float x is a NaN (not a number). For more information on NaNs, see the IEEE 754 standards.

math.ldexp(xi)
Return x * (2**i). This is essentially the inverse of function frexp().

math.modf(x)
Return the fractional and integer parts of x. Both results carry the sign of x and are floats.
>>> math.modf(1.1)
(0.10000000000000009, 1.0)
>>> math.modf(-1.1)
(-0.10000000000000009, -1.0)

math.trunc(x)
New in version 2.6.
Return the Real value x truncated to an Integral (usually a long integer). Uses the __trunc__ method.
>>> math.trunc(10.123)
10
>>> math.trunc(-10.456)
-10

Power and logarithmic functions :

math.exp(x)
Return e**x.

math.expm1(x)
New in version 2.7.
Return e**x - 1. For small floats x, the subtraction in exp(x) - 1 can result in a significant loss of precision; the expm1() function provides a way to compute this quantity to full precision :
>>> from math import exp, expm1
>>> exp(1e-5) - 1 # gives result accurate to 11 places
1.0000050000069649e-05
>>> expm1(1e-5) # result accurate to full precision
1.0000050000166668e-05

math.log(x[, base])
With one argument, return the natural logarithm of x (to base e).
With two arguments, return the logarithm of x to the given base, calculated as log(x)/log(base).
Changed in version 2.3: base argument added.
>>> a = math.log(4) # log(base)
>>> b = math.log(16) # log(x), x=16
>>> b/a # log(x)/log(base) ; x=16, base=4
2.0
>>> math.log(16, 4) # x=16, base=4
2.0

math.log1p(x)
New in version 2.6.
Return the natural logarithm of 1+x (base e). The result is calculated in a way which is accurate for x near zero.

math.log10(x)
Return the base-10 logarithm of x. This is usually more accurate than log(x, 10).

math.pow(xy)
Changed in version 2.6: The outcome of 1**nan and nan**0 was undefined.
Return x raised to the power y. Exceptional cases follow Annex ‘F’ of the C99 standard as far as possible. In particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN. If both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises ValueError.

math.sqrt(x)
Return the square root of x.

Trigonometric functions : math.acos(x)
Return the arc cosine of x, in radians.
>>> (math.acos(1/math.sqrt(2))/math.pi) * 180 # math.acos(1/2^0.5) -> 45 degree -> 3.14 * (45/180) radians
45.00000000000001

math.asin(x)
Return the arc sine of x, in radians.

math.atan(x)
Return the arc tangent of x, in radians.

math.atan2(yx)
Return atan(y / x), in radians. The result is between -pi and pi. The vector in the plane from the origin to point (xy) makes this angle with the positive X axis. The point of atan2() is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle. For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4.

math.cos(x)
Return the cosine of x radians.
>>> math.cos(math.pi/4)
0.7071067811865476
>>> math.sqrt(2)/2
0.7071067811865476

math.hypot(xy)
Return the Euclidean norm, sqrt(x*x + y*y). This is the length of the vector from the origin to point (x, y).
>>> math.hypot(1, 1)
1.4142135623730951
>>> math.sqrt(2)
1.4142135623730951

math.sin(x)
Return the sine of x radians.

math.tan(x)
Return the tangent of x radians.

Angular conversion :

math.degrees(x)
Converts angle x from radians to degrees.
>>> math.degrees(math.pi)
180.0

Converts angle x from degrees to radians.
3.141592653589793

Special functions :
math.erf(x)
New in version 2.7.
Return the error function at x.

math.erfc(x)
New in version 2.7.
Return the complementary error function at x.

math.gamma(x)
New in version 2.7.
Return the Gamma function at x.

math.lgamma(x)
New in version 2.7.
Return the natural logarithm of the absolute value of the Gamma function at x.

Constants :

math.pi
The mathematical constant π = 3.141592..., to available precision.

math.e
The mathematical constant e = 2.718281..., to available precision.
This message was edited 40 times. Last update was at 10/04/2012 09:18:20

### [ Python 文章收集 ] SQLAlchemy Core - Using Functions (7)

Source From   Here   Using Functions   Standard SQL has recommended many functions which are implemented by most dialects. They return a si...