The math Module

The math module is made available to your programs with:

import math

The math module contains the following trigonometric functions

math.acos (x ) → number

arc cosine of x.

math.asin (x ) → number

arc sine of x.

math.atan (x ) → number

arc tangent of x.

math.atan2 (y, x ) → number

arc tangent of y/x.

math.cos (x ) → number

cosine of x.

math.cosh (x ) → number

hyperbolic cosine of x.

math.exp (x ) → number

e**x, inverse of log(x).

math.hypot (x, y ) → number

Euclidean distance, sqrt(x*x + y*y), length of the hypotenuse of a right triangle with height of y and length of x.

math.log (x ) → number

natural logarithm (base e) of x, inverse of exp(x).

math.log10 (x ) → number

natural logarithm (base 10) of x, inverse of 10**x.

math.pow (x, y ) → number

x**y.

math.sin (x ) → number

sine of x.

math.sinh (x ) → number

hyperbolic sine of x.

math.sqrt (x ) → number

square root of x. This version returns an error if you ask for sqrt(-1), even though Python understands complex and imaginary numbers. A second module, cmath, includes a version of sqrt(x) which correctly creates imaginary numbers.

math.tan (x ) → number

tangent of x.

math.tanh (x ) → number

hyperbolic tangent of x.

Additionally, the following constants are also provided.

math.pi

the value of pi, 3.1415926535897931

math.e

the value of e, 2.7182818284590451, used for the exp(x) and log(x) functions.

The math module contains the following other functions for dealing with floating point numbers.

math.ceil (x ) → number

next larger whole number. math.ceil(5.1) == 6, math.ceil(-5.1) == -5.0.

math.fabs (x ) → number

absolute value of the real x.

math.floor (x ) → number

next smaller whole number. math.floor(5.9) == 5, math.floor(-5.9) == -6.0.

math.fmod (x, y ) → number

floating point remainder after division of x/y. This depends on the platform C library and may return a different result than the Python x % y.

math.modf (x ) → ( number, number )

creates a tuple with the fractional and integer parts of x. Both results carry the sign of x so that x can be reconstructed by adding them.

math.frexp (x ) → ( number, number )

this function unwinds the usual base-2 floating point representation. A floating point number is m*2**e, where m is always a fraction between 1/2 and 1, and e is an integer power of 2. This function returns a tuple with m and e. The inverse is ldexp(m,e).

math.ldexp (m, e ) → number

m*2**e, the inverse of frexp(x).