5.7 Math-- Mathematical functions
This module is always available. It provides access to the mathematical functions defined by the C standard.
These functions cannot be used with complex numbers; Use the functions of the same name fromCmathModule if you require support for complex numbers. the distinction between functions which support complex numbers and those which don't is made since most users do not want to learn quite as much mathematics as required to understand complex numbers. processing an exception instead of a complex result allows earlier detection of the unexpected complex number used as a parameter, so that the programmer can determine how and why it was generated in the first place.
The following funwing are provided by this module. When t when explicitly noted otherwise, all return values are floats.
Number-theoretic and representation functions:
-
-
Return the ceiling XAs a float, the smallest integer value greater than or equal X.
-
-
Return the absolute value X.
-
-
Return the floor XAs a float, the largest integer value less than or equal X.
-
-
Return
fmod(x, y)
, As defined by the platform C library. Note that the python expression
x % y
May not return the same result. The intent of the C standard is that
fmod(x, y)
Be exactly (mathematically; to infinite precision) equal
x - n*y
For some integer NSuch that the result has the same sign XAnd magn0000less
abs(y)
. Python's
x % y
Returns a result with the sign YInstead, and may not be exactly computable for float arguments. For example,
fmod(-1e-100, 1e100)
Is
-1e-100
, But the result of Python's
-1e-100 % 1e100
Is
1e100-1e-100
, Which cannot be represented exactly as a float, and rounds to the surprising
1e100
. For this reason, Function Fmod ()Is generally preferred when working with floats, while Python's
x % y
Is preferred when working with integers.
-
-
Return the mantissa and exponent XAs the pair
(m, e)
. MIs a float and EIs an integer such that
x == m * 2**e
Exactly. If XIs zero, returns
(0.0, 0)
, Otherwise
0.5 <= abs(m) < 1
. This is used to "pick apart" the internal representation of a float in a portable way.
-
-
Return
x * (2**i)
. This is essential, the inverse of Function Frexp ().
-
-
Return the fractional and integer parts X. Both Results carry the sign X, And both are floats.
Note thatFrexp ()AndMODF ()Have a different call/return pattern than their C equivalents: they take a single argument and return a pair of values, rather than returning their second return value through an 'output parameter '(there is no such thing in Python ).
ForCeil (),Floor (), AndMODF ()Functions, note thatAllFloating-point numbers of sufficiently large magnnames are exact integers. Python floats typically carry no more than 53 bits of precision (the same as the platform C double type), in which case any floatXWithabs(x) >= 2**52
Necessarily has no fractional bits.
Power and logarithmic functions:
-
-
Return
e**x
.
-
-
Return the logarithm XTo the given Base. If BaseIs not specified, return the natural logarithm X(That is, the logarithm to base
E). Changed in Version 2.3: BaseArgument added.
-
-
Return the base-10 logarithm X.
-
-
Return
x**y
.
-
-
Return the square root X.
Trigonometric functions:
-
-
Return the arc cosine X, In radians.
-
-
Return the arc sine X, In radians.
-
-
Return the arc tangent X, In radians.
-
-
Return
atan(y / x)
, In radians. The result is
-pi
And
pi
. The vector in the plane from the origin to Point
(x, y)
Makes this angle with the positive X axis. The point Atan2 ()Is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle. For example,
atan(1
) And
atan2(1, 1)
Are both
pi/4
,
atan2(-1, -1)
Is
-3*pi/4
.
-
-
Return the cosine XRadians.
-
-
Return the euclidean norm,
sqrt(x*x + y*y)
. This is the length of the vector from the origin to Point
(x, y)
.
-
-
Return the sine XRadians.
-
-
Return the tangent XRadians.
Angular conversion:
-
-
Converts Angle XFrom radians to degrees.
-
-
Converts Angle XFrom degrees to radians.
Hyperbolic functions:
-
-
Return the hyperbolic cosine X.
-
-
Return the hyperbolic sine X.
-
-
Return the hyperbolic tangent X.
The module also defines two mathematical constants:
-
Pi
-
The mathematical constant
Pi.
-
E
-
The mathematical constant
E.
Note:The MathModule consists mostly of thin wrappers around the platform C math library functions. behavior in exceptional cases is loosely specified by the C standards, and Python inherits much of its math-function error-reporting behavior from the platform C implementation. as a result, the specific exceptions raised in error cases (and even whether some arguments are considered to be exceptional at all) are not defined in any useful cross-platform or cross-release way. for example, whether
math.log(0)
Returns
-Inf
Or raises ValueerrorOr OverflowerrorIsn' t defined, and in cases where
math.log(0)
Raises Overflowerror,
math.log(0L)
May raise ValueerrorInstead.
See also:
-
Module
Cmath:
-
Complex Number versions of these functions.