The math module is made available to your
programs with:
import math
The math module contains the following
trigonometric functions
math.acos (x ) →
numberarc cosine of x.
math.asin (x ) →
numberarc sine of x.
math.atan (x ) →
numberarc tangent of x.
math.atan2 (y,
x ) → numberarc tangent of y/x.
math.cos (x ) →
numbercosine of x.
math.cosh (x ) →
numberhyperbolic cosine of x.
math.exp (x ) →
numbere**x, inverse of log(x).
math.hypot (x,
y ) → numberEuclidean 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 ) →
numbernatural logarithm (base e) of x, inverse of exp(x).
math.log10 (x ) →
numbernatural logarithm (base 10) of x, inverse of 10**x.
math.pow (x,
y ) → numberx**y.
math.sin (x ) →
numbersine of x.
math.sinh (x ) →
numberhyperbolic sine of x.
math.sqrt (x ) →
numbersquare 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 ) →
numbertangent of x.
math.tanh (x ) →
numberhyperbolic tangent of x.
Additionally, the following constants are also provided.
math.pithe value of pi, 3.1415926535897931
math.ethe 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 ) →
numbernext larger whole number. math.ceil(5.1) == 6,
math.ceil(-5.1) == -5.0.
math.fabs (x ) →
numberabsolute value of the real x.
math.floor (x ) →
numbernext smaller whole number. math.floor(5.9) == 5,
math.floor(-5.9) == -6.0.
math.fmod (x,
y ) → numberfloating 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 ) → numberm*2**e, the inverse
of frexp(x).