blob: e52f8f96fc05140ace995b27220a677a9792fcd8 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{math} ---
Fred Drake69fa5631999-04-21 16:29:57 +00002 Mathematical functions}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{builtin}{math}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Mathematical functions (\function{sin()} etc.).}
6
Fred Drake69fa5631999-04-21 16:29:57 +00007This module is always available. It provides access to the
Fred Drake38e5d272000-04-03 20:13:55 +00008mathematical functions defined by the C standard.
9
10These functions cannot be used with complex numbers; use the functions
11of the same name from the \refmodule{cmath} module if you require
12support for complex numbers. The distinction between functions which
13support complex numbers and those which don't is made since most users
14do not want to learn quite as much mathematics as required to
15understand complex numbers. Receiving an exception instead of a
16complex result allows earlier detection of the unexpected complex
17number used as a parameter, so that the programmer can determine how
18and why it was generated in the first place.
19
Tim Petersa1af7672003-04-28 21:32:03 +000020The following functions are provided by this module. Except
Tim Peters66bb6e62004-07-24 23:00:24 +000021when explicitly noted otherwise, all return values are floats.
Fred Drakeb55e07f1997-09-30 21:59:27 +000022
Tim Peters66bb6e62004-07-24 23:00:24 +000023Number-theoretic and representation functions:
Fred Drakeb55e07f1997-09-30 21:59:27 +000024
25\begin{funcdesc}{ceil}{x}
Tim Peters66bb6e62004-07-24 23:00:24 +000026Return the ceiling of \var{x} as a float, the smallest integer value
27greater than or equal to \var{x}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000028\end{funcdesc}
29
30\begin{funcdesc}{fabs}{x}
Tim Petersa1af7672003-04-28 21:32:03 +000031Return the absolute value of \var{x}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000032\end{funcdesc}
33
34\begin{funcdesc}{floor}{x}
Tim Peters66bb6e62004-07-24 23:00:24 +000035Return the floor of \var{x} as a float, the largest integer value
36less than or equal to \var{x}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000037\end{funcdesc}
38
39\begin{funcdesc}{fmod}{x, y}
Tim Peters78fc0b52000-09-16 03:54:24 +000040Return \code{fmod(\var{x}, \var{y})}, as defined by the platform C library.
41Note that the Python expression \code{\var{x} \%\ \var{y}} may not return
Tim Peters66bb6e62004-07-24 23:00:24 +000042the same result. The intent of the C standard is that
43\code{fmod(\var{x}, \var{y})} be exactly (mathematically; to infinite
44precision) equal to \code{\var{x} - \var{n}*\var{y}} for some integer
45\var{n} such that the result has the same sign as \var{x} and
46magnitude less than \code{abs(\var{y})}. Python's
47\code{\var{x} \%\ \var{y}} returns a result with the sign of
48\var{y} instead, and may not be exactly computable for float arguments.
49For example, \code{fmod(-1e-100, 1e100)} is \code{-1e-100}, but the
50result of Python's \code{-1e-100 \%\ 1e100} is \code{1e100-1e-100}, which
51cannot be represented exactly as a float, and rounds to the surprising
52\code{1e100}. For this reason, function \function{fmod()} is generally
53preferred when working with floats, while Python's
54\code{\var{x} \%\ \var{y}} is preferred when working with integers.
Fred Drakeb55e07f1997-09-30 21:59:27 +000055\end{funcdesc}
56
57\begin{funcdesc}{frexp}{x}
Fred Drakefcc95a42000-07-03 06:38:17 +000058Return the mantissa and exponent of \var{x} as the pair
59\code{(\var{m}, \var{e})}. \var{m} is a float and \var{e} is an
Tim Peters66bb6e62004-07-24 23:00:24 +000060integer such that \code{\var{x} == \var{m} * 2**\var{e}} exactly.
Fred Drakefcc95a42000-07-03 06:38:17 +000061If \var{x} is zero, returns \code{(0.0, 0)}, otherwise
Tim Peters66bb6e62004-07-24 23:00:24 +000062\code{0.5 <= abs(\var{m}) < 1}. This is used to "pick apart" the
63internal representation of a float in a portable way.
Fred Drakeb55e07f1997-09-30 21:59:27 +000064\end{funcdesc}
65
66\begin{funcdesc}{ldexp}{x, i}
Tim Peters66bb6e62004-07-24 23:00:24 +000067Return \code{\var{x} * (2**\var{i})}. This is essentially the inverse of
68function \function{frexp()}.
Fred Drake7c418ed1998-01-22 17:37:50 +000069\end{funcdesc}
70
Fred Drakeb55e07f1997-09-30 21:59:27 +000071\begin{funcdesc}{modf}{x}
72Return the fractional and integer parts of \var{x}. Both results
Tim Peters66bb6e62004-07-24 23:00:24 +000073carry the sign of \var{x}, and both are floats.
Fred Drakeb55e07f1997-09-30 21:59:27 +000074\end{funcdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075
Fred Drake7c418ed1998-01-22 17:37:50 +000076Note that \function{frexp()} and \function{modf()} have a different
Fred Drake69fa5631999-04-21 16:29:57 +000077call/return pattern than their C equivalents: they take a single
Fred Drake7c418ed1998-01-22 17:37:50 +000078argument and return a pair of values, rather than returning their
79second return value through an `output parameter' (there is no such
80thing in Python).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081
Tim Petersabba5c02004-07-26 05:12:01 +000082For the \function{ceil()}, \function{floor()}, and \function{modf()}
83functions, note that \emph{all} floating-point numbers of sufficiently
84large magnitude are exact integers. Python floats typically carry no more
85than 53 bits of precision (the same as the platform C double type), in
86which case any float \var{x} with \code{abs(\var{x}) >= 2**52}
87necessarily has no fractional bits.
88
89
Tim Peters66bb6e62004-07-24 23:00:24 +000090Power and logarithmic functions:
91
92\begin{funcdesc}{exp}{x}
93Return \code{e**\var{x}}.
94\end{funcdesc}
95
96\begin{funcdesc}{log}{x\optional{, base}}
97Return the logarithm of \var{x} to the given \var{base}.
98If the \var{base} is not specified, return the natural logarithm of \var{x}
99(that is, the logarithm to base \emph{e}).
100\versionchanged[\var{base} argument added]{2.3}
101\end{funcdesc}
102
103\begin{funcdesc}{log10}{x}
104Return the base-10 logarithm of \var{x}.
105\end{funcdesc}
106
107\begin{funcdesc}{pow}{x, y}
108Return \code{\var{x}**\var{y}}.
109\end{funcdesc}
110
111\begin{funcdesc}{sqrt}{x}
112Return the square root of \var{x}.
113\end{funcdesc}
114
115Trigonometric functions:
116
117\begin{funcdesc}{acos}{x}
118Return the arc cosine of \var{x}, in radians.
119\end{funcdesc}
120
121\begin{funcdesc}{asin}{x}
122Return the arc sine of \var{x}, in radians.
123\end{funcdesc}
124
125\begin{funcdesc}{atan}{x}
126Return the arc tangent of \var{x}, in radians.
127\end{funcdesc}
128
129\begin{funcdesc}{atan2}{y, x}
130Return \code{atan(\var{y} / \var{x})}, in radians.
131The result is between \code{-pi} and \code{pi}.
132The vector in the plane from the origin to point \code{(\var{x}, \var{y})}
133makes this angle with the positive X axis.
134The point of \function{atan2()} is that the signs of both inputs are
135known to it, so it can compute the correct quadrant for the angle.
136For example, \code{atan(1}) and \code{atan2(1, 1)} are both \code{pi/4},
137but \code{atan2(-1, -1)} is \code{-3*pi/4}.
138\end{funcdesc}
139
140\begin{funcdesc}{cos}{x}
141Return the cosine of \var{x} radians.
142\end{funcdesc}
143
144\begin{funcdesc}{hypot}{x, y}
145Return the Euclidean norm, \code{sqrt(\var{x}*\var{x} + \var{y}*\var{y})}.
146This is the length of the vector from the origin to point
147\code{(\var{x}, \var{y})}.
148\end{funcdesc}
149
150\begin{funcdesc}{sin}{x}
151Return the sine of \var{x} radians.
152\end{funcdesc}
153
154\begin{funcdesc}{tan}{x}
155Return the tangent of \var{x} radians.
156\end{funcdesc}
157
158Angular conversion:
159
160\begin{funcdesc}{degrees}{x}
161Converts angle \var{x} from radians to degrees.
162\end{funcdesc}
163
164\begin{funcdesc}{radians}{x}
165Converts angle \var{x} from degrees to radians.
166\end{funcdesc}
167
Tim Peters9a729a12004-07-26 04:58:50 +0000168Hyperbolic functions:
Tim Peters66bb6e62004-07-24 23:00:24 +0000169
170\begin{funcdesc}{cosh}{x}
171Return the hyperbolic cosine of \var{x}.
172\end{funcdesc}
173
174\begin{funcdesc}{sinh}{x}
175Return the hyperbolic sine of \var{x}.
176\end{funcdesc}
177
178\begin{funcdesc}{tanh}{x}
179Return the hyperbolic tangent of \var{x}.
180\end{funcdesc}
181
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000182The module also defines two mathematical constants:
Guido van Rossume47da0a1997-07-17 16:34:52 +0000183
Fred Drakeb55e07f1997-09-30 21:59:27 +0000184\begin{datadesc}{pi}
185The mathematical constant \emph{pi}.
186\end{datadesc}
187
188\begin{datadesc}{e}
189The mathematical constant \emph{e}.
190\end{datadesc}
191
Skip Montanaro51183412003-04-26 02:59:00 +0000192\begin{notice}
Tim Peters965697f2003-04-26 15:11:08 +0000193 The \module{math} module consists mostly of thin wrappers around
194 the platform C math library functions. Behavior in exceptional cases is
195 loosely specified by the C standards, and Python inherits much of its
196 math-function error-reporting behavior from the platform C
197 implementation. As a result,
198 the specific exceptions raised in error cases (and even whether some
Skip Montanaro51183412003-04-26 02:59:00 +0000199 arguments are considered to be exceptional at all) are not defined in any
200 useful cross-platform or cross-release way. For example, whether
201 \code{math.log(0)} returns \code{-Inf} or raises \exception{ValueError} or
Tim Peters965697f2003-04-26 15:11:08 +0000202 \exception{OverflowError} isn't defined, and in
203 cases where \code{math.log(0)} raises \exception{OverflowError},
204 \code{math.log(0L)} may raise \exception{ValueError} instead.
Skip Montanaro51183412003-04-26 02:59:00 +0000205\end{notice}
206
Fred Drake2950b2d1997-10-13 22:06:17 +0000207\begin{seealso}
208 \seemodule{cmath}{Complex number versions of many of these functions.}
209\end{seealso}