Previous Section Next Section

15.1 The math and cmath Modules

The math module supplies mathematical functions on floating-point numbers, while the cmath module supplies equivalent functions on complex numbers. For example, math.sqrt(-1) raises an exception, but cmath.sqrt(-1) returns 1j.

Each module also exposes two attributes of type float bound to the values of fundamental mathematical constants, pi and e.

acos math and cmath

acos(x)

Returns the arccosine of x in radians.

acosh cmath only

acosh(x)

Returns the arc hyperbolic cosine of x in radians.

asin math and cmath

asin(x)

Returns the arcsine of x in radians.

asinh cmath only

asinh(x)

Returns the arc hyperbolic sine of x in radians.

atan math and cmath

atan(x)

Returns the arctangent of x in radians.

atanh cmath only

atanh(x)

Returns the arc hyperbolic tangent of x in radians.

atan2 math only

atan2(y,x)

Like atan(y/x), except that when x equals 0, atan2 returns pi/2, while dividing by x would raise ZeroDivisionError.

ceil math only

ceil(x)

Returns the lowest integer i such that i is greater than or equal to x as a floating-point value.

cos math and cmath

cos(x)

Returns the cosine of x in radians.

cosh math and cmath

cosh(x)

Returns the hyperbolic cosine of x in radians.

e math and cmath

The mathematical constant e.

exp math and cmath

exp(x)

Returns e**x.

fabs math only

fabs(x)

Returns the absolute value of x.

floor math only

floor(x)

Returns the highest integer i such that i is less than or equal to x as a floating-point value.

fmod math only

fmod(x,y)

Returns x%y (on most platforms).

frexp math only

frexp(x)

Returns a pair (m,e) with the mantissa and exponent of x. m is a floating-point number and e is an integer such that x= =m*(2**e) and 0.5<=abs(m)<1, except that frexp(0) returns (0.0,0).

hypot math only

hypot(x,y)

Returns sqrt(x*x+y*y).

ldexp math only

ldexp(x,i)

Returns x*(2**i).

log math and cmath

log(x)

Returns the natural logarithm of x.

log10 math and cmath

log10(x)

Returns the base-10 logarithm of x.

modf math only

modf(x)

Returns a pair (f,i) with fractional and integer parts of x, each a floating-point value with the same sign as x.

pi math and cmath

The mathematical constant figs/U03C0.gif.

pow math only

pow(x,y)

Returns x**y.

sin math and cmath

sin(x)

Returns the sine of x in radians.

sinh math and cmath

sinh(x)

Returns the hyperbolic sine of x in radians.

sqrt math and cmath

sqrt(x)

Returns the square root of x.

tan math and cmath

tan(x)

Returns the tangent of x in radians.

tanh math and cmath

tanh(x)

Returns the hyperbolic tangent of x in radians.

    Previous Section Next Section