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)

Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.