Mathematical methods
From the math
module you can access useful mathematical methods. Some of these
methods are listed in the following table. Unless specified otherwise, all values are returned as
floats.
Method | Usage |
---|---|
math.ceil(x) |
Return the ceiling of x as
a float, that is the smallest integer greater than or equal to x |
math.copysign(x, y) |
Return x with the sign of y . copysign(1,
-0.0) returns -1 |
math.fabs(x) |
Return the absolute value of x |
math.factorial(x) |
Return x factorial. If x is
negative or not an integer, a ValueError is raised. |
math.floor(x) |
Return the floor of x as a
float, that is the largest integer less than or equal to x |
math.frexp(x) |
Return the mantissa (m ) and
exponent (e ) of x as the pair (m,
e) . m is a float and e is
an integer, such that x == m * 2**e exactly. If x is
zero, returns (0.0, 0) , otherwise 0.5 <=
abs(m) < 1 . |
math.fsum(iterable) |
Return an accurate floating point sum of values
in iterable |
math.isinf(x) |
Check if the float x is positive
or negative infinitive |
math.isnan(x) |
Check if the float x is NaN (not
a number) |
math.ldexp(x, i) |
Return x * (2**i) . This is
essentially the inverse of the function frexp . |
math.modf(x) |
Return the fractional and integer parts of x .
Both results carry the sign of x and are floats. |
math.trunc(x) |
Return the Real value x ,
that has been truncated to an Integral . |
math.exp(x) |
Return e**x |
math.log(x[, base]) |
Return the logarithm of x to
the given value of base . If base is
not specified, the natural logarithm of x is returned. |
math.log1p(x) |
Return the natural logarithm of 1+x
(base e) |
math.log10(x) |
Return the base-10 logarithm of x |
math.pow(x, y) |
Return x raised to the power y . pow(1.0,
x) and pow(x, 0.0) always return 1 ,
even when x is zero or NaN. |
math.sqrt(x) |
Return the square root of x |
Along with the mathematical functions, there are also some useful trigonometric methods. These methods are listed in the following table.
Method | Usage |
---|---|
math.acos(x) |
Return the arc cosine of x in
radians |
math.asin(x) |
Return the arc sine of x in
radians |
math.atan(x) |
Return the arc tangent of x in
radians |
math.atan2(y, x) |
Return atan(y / x) in radians. |
math.cos(x) |
Return the cosine of x in radians. |
math.hypot(x, y) |
Return the Euclidean norm sqrt(x*x +
y*y) . This is the length of the vector from the origin to
the point (x, y) . |
math.sin(x) |
Return the sine of x in radians |
math.tan(x) |
Return the tangent of x in
radians |
math.degrees(x) |
Convert angle x from radians
to degrees |
math.radians(x) |
Convert angle x from degrees
to radians |
math.acosh(x) |
Return the inverse hyperbolic cosine of x |
math.asinh(x) |
Return the inverse hyperbolic sine of x |
math.atanh(x) |
Return the inverse hyperbolic tangent of x |
math.cosh(x) |
Return the hyperbolic cosine of x |
math.sinh(x) |
Return the hyperbolic cosine of x |
math.tanh(x) |
Return the hyperbolic tangent of x |
There are also two mathematical constants. The value of math.pi
is
the mathematical constant pi. The value of math.e
is
the mathematical constant e.