math — Mathematical functions¶
This module is always available. It provides access to the mathematicalfunctions defined by the C standard.
These functions cannot be used with complex numbers; use the functions of thesame name from thecmath module if you require support for complexnumbers. The distinction between functions which support complex numbers andthose which don’t is made since most users do not want to learn quite as muchmathematics as required to understand complex numbers. Receiving an exceptioninstead of a complex result allows earlier detection of the unexpected complexnumber used as a parameter, so that the programmer can determine how and why itwas generated in the first place.
The following functions are provided by this module. Except when explicitlynoted otherwise, all return values are floats.
Number-theoretic and representation functions¶
- math.ceil(x)¶
- Return the ceiling ofx, the smallest integer greater than or equal tox.Ifx is not a float, delegates tox.__ceil__(), which should return anIntegral value.
- math.copysign(x,y)¶
- Returnx with the sign ofy.copysign copies the sign bit of an IEEE754 float,copysign(1,-0.0) returns-1.0.
- math.fabs(x)¶
- Return the absolute value ofx.
- math.factorial(x)¶
- Returnx factorial. RaisesValueError ifx is not integral oris negative.
- math.floor(x)¶
- Return the floor ofx, the largest integer less than or equal tox.Ifx is not a float, delegates tox.__floor__(), which should return anIntegral value.
- math.fmod(x,y)¶
- Returnfmod(x,y), as defined by the platform C library. Note that thePython expressionx%y may not return the same result. The intent of the Cstandard is thatfmod(x,y) be exactly (mathematically; to infiniteprecision) equal tox-n*y for some integern such that the result hasthe same sign asx and magnitude less thanabs(y). Python’sx%yreturns a result with the sign ofy instead, and may not be exactly computablefor float arguments. For example,fmod(-1e-100,1e100) is-1e-100, butthe result of Python’s-1e-100%1e100 is1e100-1e-100, which cannot berepresented exactly as a float, and rounds to the surprising1e100. Forthis reason, functionfmod() is generally preferred when working withfloats, while Python’sx%y is preferred when working with integers.
- math.frexp(x)¶
- Return the mantissa and exponent ofx as the pair(m,e).m is a floatande is an integer such thatx==m*2**e exactly. Ifx is zero,returns(0.0,0), otherwise0.5<=abs(m)<1. This is used to “pickapart” the internal representation of a float in a portable way.
- math.fsum(iterable)¶
Return an accurate floating point sum of values in the iterable. Avoidsloss of precision by tracking multiple intermediate partial sums. Thealgorithm’s accuracy depends on IEEE-754 arithmetic guarantees and thetypical case where the rounding mode is half-even.
Note
The accuracy of fsum() may be impaired on builds that useextended precision addition and then double-round the results.
- math.isinf(x)¶
- Checks if the floatx is positive or negative infinite.
- math.isnan(x)¶
- Checks if the floatx is a NaN (not a number). NaNs are part of theIEEE 754 standards. Operation like but not limited toinf*0,inf/inf or any operation involving a NaN, e.g.nan*1, returna NaN.
- math.ldexp(x,i)¶
- Returnx*(2**i). This is essentially the inverse of functionfrexp().
- math.modf(x)¶
- Return the fractional and integer parts ofx. Both results carry the signofx and are floats.
- math.trunc(x)¶
- Return theReal valuex truncated to anIntegral (usuallyan integer). Delegates tox.__trunc__().
Note thatfrexp() andmodf() have a different call/return patternthan their C equivalents: they take a single argument and return a pair ofvalues, rather than returning their second return value through an ‘outputparameter’ (there is no such thing in Python).
For theceil(),floor(), andmodf() functions, note thatallfloating-point numbers of sufficiently large magnitude are exact integers.Python floats typically carry no more than 53 bits of precision (the same as theplatform C double type), in which case any floatx withabs(x)>=2**52necessarily has no fractional bits.
Power and logarithmic functions¶
- math.exp(x)¶
- Returne**x.
- math.log(x[,base])¶
- Return the logarithm ofx to the givenbase. If thebase is not specified,return the natural logarithm ofx (that is, the logarithm to basee).
- math.log1p(x)¶
- Return the natural logarithm of1+x (basee). Theresult is calculated in a way which is accurate forx near zero.
- math.log10(x)¶
- Return the base-10 logarithm ofx.
- math.pow(x,y)¶
- Returnx raised to the powery. Exceptional cases followAnnex ‘F’ of the C99 standard as far as possible. In particular,pow(1.0,x) andpow(x,0.0) always return1.0, evenwhenx is a zero or a NaN. If bothx andy are finite,x is negative, andy is not an integer thenpow(x,y)is undefined, and raisesValueError.
- math.sqrt(x)¶
- Return the square root ofx.
Trigonometric functions¶
- math.acos(x)¶
- Return the arc cosine ofx, in radians.
- math.asin(x)¶
- Return the arc sine ofx, in radians.
- math.atan(x)¶
- Return the arc tangent ofx, in radians.
- math.atan2(y,x)¶
- Returnatan(y/x), in radians. The result is between-pi andpi.The vector in the plane from the origin to point(x,y) makes this anglewith the positive X axis. The point ofatan2() is that the signs of bothinputs are known to it, so it can compute the correct quadrant for the angle.For example,atan(1) andatan2(1,1) are bothpi/4, butatan2(-1,-1) is-3*pi/4.
- math.cos(x)¶
- Return the cosine ofx radians.
- math.hypot(x,y)¶
- Return the Euclidean norm,sqrt(x*x+y*y). This is the length of the vectorfrom the origin to point(x,y).
- math.sin(x)¶
- Return the sine ofx radians.
- math.tan(x)¶
- Return the tangent ofx radians.
Angular conversion¶
- math.degrees(x)¶
- Converts anglex from radians to degrees.
- math.radians(x)¶
- Converts anglex from degrees to radians.
Hyperbolic functions¶
- math.acosh(x)¶
- Return the inverse hyperbolic cosine ofx.
- math.asinh(x)¶
- Return the inverse hyperbolic sine ofx.
- math.atanh(x)¶
- Return the inverse hyperbolic tangent ofx.
- math.cosh(x)¶
- Return the hyperbolic cosine ofx.
- math.sinh(x)¶
- Return the hyperbolic sine ofx.
- math.tanh(x)¶
- Return the hyperbolic tangent ofx.
Constants¶
- math.pi¶
- The mathematical constantpi.
- math.e¶
- The mathematical constante.
Note
Themath module consists mostly of thin wrappers around the platform Cmath library functions. Behavior in exceptional cases is loosely specifiedby the C standards, and Python inherits much of its math-functionerror-reporting behavior from the platform C implementation. As a result,the specific exceptions raised in error cases (and even whether somearguments are considered to be exceptional at all) are not defined in anyuseful cross-platform or cross-release way. For example, whethermath.log(0) returns-Inf or raisesValueError orOverflowError isn’t defined, and in cases wheremath.log(0) raisesOverflowError,math.log(0L) may raiseValueError instead.
All functions return a quietNaN if at least one of the args isNaN.SignalingNaNs raise an exception. The exception type still depends on theplatform and libm implementation. It’s usuallyValueError forEDOMandOverflowError for errnoERANGE.
See also
- Modulecmath
- Complex number versions of many of these functions.