source: vendor/python/2.5/Doc/lib/libmath.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 7.1 KB
Line 
1\section{\module{math} ---
2 Mathematical functions}
3
4\declaremodule{builtin}{math}
5\modulesynopsis{Mathematical functions (\function{sin()} etc.).}
6
7This module is always available. It provides access to the
8mathematical 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
20The following functions are provided by this module. Except
21when explicitly noted otherwise, all return values are floats.
22
23Number-theoretic and representation functions:
24
25\begin{funcdesc}{ceil}{x}
26Return the ceiling of \var{x} as a float, the smallest integer value
27greater than or equal to \var{x}.
28\end{funcdesc}
29
30\begin{funcdesc}{fabs}{x}
31Return the absolute value of \var{x}.
32\end{funcdesc}
33
34\begin{funcdesc}{floor}{x}
35Return the floor of \var{x} as a float, the largest integer value
36less than or equal to \var{x}.
37\end{funcdesc}
38
39\begin{funcdesc}{fmod}{x, y}
40Return \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
42the 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.
55\end{funcdesc}
56
57\begin{funcdesc}{frexp}{x}
58Return 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
60integer such that \code{\var{x} == \var{m} * 2**\var{e}} exactly.
61If \var{x} is zero, returns \code{(0.0, 0)}, otherwise
62\code{0.5 <= abs(\var{m}) < 1}. This is used to "pick apart" the
63internal representation of a float in a portable way.
64\end{funcdesc}
65
66\begin{funcdesc}{ldexp}{x, i}
67Return \code{\var{x} * (2**\var{i})}. This is essentially the inverse of
68function \function{frexp()}.
69\end{funcdesc}
70
71\begin{funcdesc}{modf}{x}
72Return the fractional and integer parts of \var{x}. Both results
73carry the sign of \var{x}, and both are floats.
74\end{funcdesc}
75
76Note that \function{frexp()} and \function{modf()} have a different
77call/return pattern than their C equivalents: they take a single
78argument and return a pair of values, rather than returning their
79second return value through an `output parameter' (there is no such
80thing in Python).
81
82For 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
90Power 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
168Hyperbolic functions:
169
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
182The module also defines two mathematical constants:
183
184\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
192\begin{notice}
193 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
199 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
202 \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.
205\end{notice}
206
207\begin{seealso}
208 \seemodule{cmath}{Complex number versions of many of these functions.}
209\end{seealso}
Note: See TracBrowser for help on using the repository browser.