blob: 7f25eba67586ee0b76cc5d0fbdb7e4a7fb3c03e8 [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 Peters66bb6e62004-07-24 23:00:24 +000082Power and logarithmic functions:
83
84\begin{funcdesc}{exp}{x}
85Return \code{e**\var{x}}.
86\end{funcdesc}
87
88\begin{funcdesc}{log}{x\optional{, base}}
89Return the logarithm of \var{x} to the given \var{base}.
90If the \var{base} is not specified, return the natural logarithm of \var{x}
91(that is, the logarithm to base \emph{e}).
92\versionchanged[\var{base} argument added]{2.3}
93\end{funcdesc}
94
95\begin{funcdesc}{log10}{x}
96Return the base-10 logarithm of \var{x}.
97\end{funcdesc}
98
99\begin{funcdesc}{pow}{x, y}
100Return \code{\var{x}**\var{y}}.
101\end{funcdesc}
102
103\begin{funcdesc}{sqrt}{x}
104Return the square root of \var{x}.
105\end{funcdesc}
106
107Trigonometric functions:
108
109\begin{funcdesc}{acos}{x}
110Return the arc cosine of \var{x}, in radians.
111\end{funcdesc}
112
113\begin{funcdesc}{asin}{x}
114Return the arc sine of \var{x}, in radians.
115\end{funcdesc}
116
117\begin{funcdesc}{atan}{x}
118Return the arc tangent of \var{x}, in radians.
119\end{funcdesc}
120
121\begin{funcdesc}{atan2}{y, x}
122Return \code{atan(\var{y} / \var{x})}, in radians.
123The result is between \code{-pi} and \code{pi}.
124The vector in the plane from the origin to point \code{(\var{x}, \var{y})}
125makes this angle with the positive X axis.
126The point of \function{atan2()} is that the signs of both inputs are
127known to it, so it can compute the correct quadrant for the angle.
128For example, \code{atan(1}) and \code{atan2(1, 1)} are both \code{pi/4},
129but \code{atan2(-1, -1)} is \code{-3*pi/4}.
130\end{funcdesc}
131
132\begin{funcdesc}{cos}{x}
133Return the cosine of \var{x} radians.
134\end{funcdesc}
135
136\begin{funcdesc}{hypot}{x, y}
137Return the Euclidean norm, \code{sqrt(\var{x}*\var{x} + \var{y}*\var{y})}.
138This is the length of the vector from the origin to point
139\code{(\var{x}, \var{y})}.
140\end{funcdesc}
141
142\begin{funcdesc}{sin}{x}
143Return the sine of \var{x} radians.
144\end{funcdesc}
145
146\begin{funcdesc}{tan}{x}
147Return the tangent of \var{x} radians.
148\end{funcdesc}
149
150Angular conversion:
151
152\begin{funcdesc}{degrees}{x}
153Converts angle \var{x} from radians to degrees.
154\end{funcdesc}
155
156\begin{funcdesc}{radians}{x}
157Converts angle \var{x} from degrees to radians.
158\end{funcdesc}
159
Tim Peters9a729a12004-07-26 04:58:50 +0000160Hyperbolic functions:
Tim Peters66bb6e62004-07-24 23:00:24 +0000161
162\begin{funcdesc}{cosh}{x}
163Return the hyperbolic cosine of \var{x}.
164\end{funcdesc}
165
166\begin{funcdesc}{sinh}{x}
167Return the hyperbolic sine of \var{x}.
168\end{funcdesc}
169
170\begin{funcdesc}{tanh}{x}
171Return the hyperbolic tangent of \var{x}.
172\end{funcdesc}
173
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000174The module also defines two mathematical constants:
Guido van Rossume47da0a1997-07-17 16:34:52 +0000175
Fred Drakeb55e07f1997-09-30 21:59:27 +0000176\begin{datadesc}{pi}
177The mathematical constant \emph{pi}.
178\end{datadesc}
179
180\begin{datadesc}{e}
181The mathematical constant \emph{e}.
182\end{datadesc}
183
Skip Montanaro51183412003-04-26 02:59:00 +0000184\begin{notice}
Tim Peters965697f2003-04-26 15:11:08 +0000185 The \module{math} module consists mostly of thin wrappers around
186 the platform C math library functions. Behavior in exceptional cases is
187 loosely specified by the C standards, and Python inherits much of its
188 math-function error-reporting behavior from the platform C
189 implementation. As a result,
190 the specific exceptions raised in error cases (and even whether some
Skip Montanaro51183412003-04-26 02:59:00 +0000191 arguments are considered to be exceptional at all) are not defined in any
192 useful cross-platform or cross-release way. For example, whether
193 \code{math.log(0)} returns \code{-Inf} or raises \exception{ValueError} or
Tim Peters965697f2003-04-26 15:11:08 +0000194 \exception{OverflowError} isn't defined, and in
195 cases where \code{math.log(0)} raises \exception{OverflowError},
196 \code{math.log(0L)} may raise \exception{ValueError} instead.
Skip Montanaro51183412003-04-26 02:59:00 +0000197\end{notice}
198
Fred Drake2950b2d1997-10-13 22:06:17 +0000199\begin{seealso}
200 \seemodule{cmath}{Complex number versions of many of these functions.}
201\end{seealso}