Previous Page
Next Page

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 exposes two attributes of type float bound to the values of fundamental mathematical constants, pi and e, and the following functions.

acos

acos(x)

Returns the arccosine of x in radians.

math and cmath

acosh

acosh(x)

Returns the arc hyperbolic cosine of x in radians.

cmath only

asin

asin(x)

Returns the arcsine of x in radians.

math and cmath

asinh

asinh(x)

Returns the arc hyperbolic sine of x in radians.

cmath only

atan

atan(x)

Returns the arctangent of x in radians.

math and cmath

atanh

atanh(x)

Returns the arc hyperbolic tangent of x in radians.

cmath only

atan2

atan2(y,x)

Like atan(y/x), except that atan2 properly takes into account the signs of both arguments. For example:

>>> import math
>>> math.atan(-1./-1.)
0.78539816339744828
>>> math.atan2(-1., -1.)
-2.3561944901923448

Also, when x equals 0, atan2 returns pi/2, while dividing by x would raise ZeroDivisionError.

math only

ceil

ceil(x)

Returns float(i), where i is the lowest integer such that i >= x.

math only

cos

cos(x)

Returns the cosine of x in radians.

math and cmath

cosh

cosh(x)

Returns the hyperbolic cosine of x in radians.

math and cmath

e

The mathematical constant e.

math and cmath

exp

exp(x)

Returns e**x.

math and cmath

fabs

fabs(x)

Returns the absolute value of x.

math only

floor

floor(x)

Returns float(i), where i is the highest integer such that i <= x.

math only

fmod

fmod(x,y)

Returns the float r, with the same sign as x, such that r==x-n*y for some integer n, and abs(r)<abs(y). Like x%y, except that, when x and y differ in sign, x%y has the same sign as y, not the same sign as x.

math only

frexp

frexp(x)

Returns a pair (m,e) with the so-called "mantissa" (in fact, the significand) 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).

math only

hypot

hypot(x,y)

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

math only

ldexp

ldexp(x,i)

Returns x*(2**i) (i should be an int; if i is a float, i gets truncated, but that situation also produces a warning).

math only

log

log(x)

Returns the natural logarithm of x.

math and cmath

log10

log10(x)

Returns the base-10 logarithm of x.

math and cmath

modf

modf(x)

Returns a pair (f,i) with fractional and integer parts of x, meaning two floats with the same sign as x such that i==int(i) and x==f+i.

math only

pi

The mathematical constant π.

math and cmath

pow

pow(x,y)

Returns x**y.

math only

sin

sin(x)

Returns the sine of x in radians.

math and cmath

sinh

sinh(x)

Returns the hyperbolic sine of x in radians.

math and cmath

sqrt

sqrt(x)

Returns the square root of x.

math and cmath

tan

tan(x)

Returns the tangent of x in radians.

math and cmath

tanh

tanh(x)

Returns the hyperbolic tangent of x in radians.

math and cmath



Previous Page
Next Page