1 | \section{\module{math} ---
|
---|
2 | Mathematical functions}
|
---|
3 |
|
---|
4 | \declaremodule{builtin}{math}
|
---|
5 | \modulesynopsis{Mathematical functions (\function{sin()} etc.).}
|
---|
6 |
|
---|
7 | This module is always available. It provides access to the
|
---|
8 | mathematical functions defined by the C standard.
|
---|
9 |
|
---|
10 | These functions cannot be used with complex numbers; use the functions
|
---|
11 | of the same name from the \refmodule{cmath} module if you require
|
---|
12 | support for complex numbers. The distinction between functions which
|
---|
13 | support complex numbers and those which don't is made since most users
|
---|
14 | do not want to learn quite as much mathematics as required to
|
---|
15 | understand complex numbers. Receiving an exception instead of a
|
---|
16 | complex result allows earlier detection of the unexpected complex
|
---|
17 | number used as a parameter, so that the programmer can determine how
|
---|
18 | and why it was generated in the first place.
|
---|
19 |
|
---|
20 | The following functions are provided by this module. Except
|
---|
21 | when explicitly noted otherwise, all return values are floats.
|
---|
22 |
|
---|
23 | Number-theoretic and representation functions:
|
---|
24 |
|
---|
25 | \begin{funcdesc}{ceil}{x}
|
---|
26 | Return the ceiling of \var{x} as a float, the smallest integer value
|
---|
27 | greater than or equal to \var{x}.
|
---|
28 | \end{funcdesc}
|
---|
29 |
|
---|
30 | \begin{funcdesc}{fabs}{x}
|
---|
31 | Return the absolute value of \var{x}.
|
---|
32 | \end{funcdesc}
|
---|
33 |
|
---|
34 | \begin{funcdesc}{floor}{x}
|
---|
35 | Return the floor of \var{x} as a float, the largest integer value
|
---|
36 | less than or equal to \var{x}.
|
---|
37 | \end{funcdesc}
|
---|
38 |
|
---|
39 | \begin{funcdesc}{fmod}{x, y}
|
---|
40 | Return \code{fmod(\var{x}, \var{y})}, as defined by the platform C library.
|
---|
41 | Note that the Python expression \code{\var{x} \%\ \var{y}} may not return
|
---|
42 | the same result. The intent of the C standard is that
|
---|
43 | \code{fmod(\var{x}, \var{y})} be exactly (mathematically; to infinite
|
---|
44 | precision) 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
|
---|
46 | magnitude 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.
|
---|
49 | For example, \code{fmod(-1e-100, 1e100)} is \code{-1e-100}, but the
|
---|
50 | result of Python's \code{-1e-100 \%\ 1e100} is \code{1e100-1e-100}, which
|
---|
51 | cannot be represented exactly as a float, and rounds to the surprising
|
---|
52 | \code{1e100}. For this reason, function \function{fmod()} is generally
|
---|
53 | preferred 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}
|
---|
58 | Return 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
|
---|
60 | integer such that \code{\var{x} == \var{m} * 2**\var{e}} exactly.
|
---|
61 | If \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
|
---|
63 | internal representation of a float in a portable way.
|
---|
64 | \end{funcdesc}
|
---|
65 |
|
---|
66 | \begin{funcdesc}{ldexp}{x, i}
|
---|
67 | Return \code{\var{x} * (2**\var{i})}. This is essentially the inverse of
|
---|
68 | function \function{frexp()}.
|
---|
69 | \end{funcdesc}
|
---|
70 |
|
---|
71 | \begin{funcdesc}{modf}{x}
|
---|
72 | Return the fractional and integer parts of \var{x}. Both results
|
---|
73 | carry the sign of \var{x}, and both are floats.
|
---|
74 | \end{funcdesc}
|
---|
75 |
|
---|
76 | Note that \function{frexp()} and \function{modf()} have a different
|
---|
77 | call/return pattern than their C equivalents: they take a single
|
---|
78 | argument and return a pair of values, rather than returning their
|
---|
79 | second return value through an `output parameter' (there is no such
|
---|
80 | thing in Python).
|
---|
81 |
|
---|
82 | For the \function{ceil()}, \function{floor()}, and \function{modf()}
|
---|
83 | functions, note that \emph{all} floating-point numbers of sufficiently
|
---|
84 | large magnitude are exact integers. Python floats typically carry no more
|
---|
85 | than 53 bits of precision (the same as the platform C double type), in
|
---|
86 | which case any float \var{x} with \code{abs(\var{x}) >= 2**52}
|
---|
87 | necessarily has no fractional bits.
|
---|
88 |
|
---|
89 |
|
---|
90 | Power and logarithmic functions:
|
---|
91 |
|
---|
92 | \begin{funcdesc}{exp}{x}
|
---|
93 | Return \code{e**\var{x}}.
|
---|
94 | \end{funcdesc}
|
---|
95 |
|
---|
96 | \begin{funcdesc}{log}{x\optional{, base}}
|
---|
97 | Return the logarithm of \var{x} to the given \var{base}.
|
---|
98 | If 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}
|
---|
104 | Return the base-10 logarithm of \var{x}.
|
---|
105 | \end{funcdesc}
|
---|
106 |
|
---|
107 | \begin{funcdesc}{pow}{x, y}
|
---|
108 | Return \code{\var{x}**\var{y}}.
|
---|
109 | \end{funcdesc}
|
---|
110 |
|
---|
111 | \begin{funcdesc}{sqrt}{x}
|
---|
112 | Return the square root of \var{x}.
|
---|
113 | \end{funcdesc}
|
---|
114 |
|
---|
115 | Trigonometric functions:
|
---|
116 |
|
---|
117 | \begin{funcdesc}{acos}{x}
|
---|
118 | Return the arc cosine of \var{x}, in radians.
|
---|
119 | \end{funcdesc}
|
---|
120 |
|
---|
121 | \begin{funcdesc}{asin}{x}
|
---|
122 | Return the arc sine of \var{x}, in radians.
|
---|
123 | \end{funcdesc}
|
---|
124 |
|
---|
125 | \begin{funcdesc}{atan}{x}
|
---|
126 | Return the arc tangent of \var{x}, in radians.
|
---|
127 | \end{funcdesc}
|
---|
128 |
|
---|
129 | \begin{funcdesc}{atan2}{y, x}
|
---|
130 | Return \code{atan(\var{y} / \var{x})}, in radians.
|
---|
131 | The result is between \code{-pi} and \code{pi}.
|
---|
132 | The vector in the plane from the origin to point \code{(\var{x}, \var{y})}
|
---|
133 | makes this angle with the positive X axis.
|
---|
134 | The point of \function{atan2()} is that the signs of both inputs are
|
---|
135 | known to it, so it can compute the correct quadrant for the angle.
|
---|
136 | For example, \code{atan(1}) and \code{atan2(1, 1)} are both \code{pi/4},
|
---|
137 | but \code{atan2(-1, -1)} is \code{-3*pi/4}.
|
---|
138 | \end{funcdesc}
|
---|
139 |
|
---|
140 | \begin{funcdesc}{cos}{x}
|
---|
141 | Return the cosine of \var{x} radians.
|
---|
142 | \end{funcdesc}
|
---|
143 |
|
---|
144 | \begin{funcdesc}{hypot}{x, y}
|
---|
145 | Return the Euclidean norm, \code{sqrt(\var{x}*\var{x} + \var{y}*\var{y})}.
|
---|
146 | This 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}
|
---|
151 | Return the sine of \var{x} radians.
|
---|
152 | \end{funcdesc}
|
---|
153 |
|
---|
154 | \begin{funcdesc}{tan}{x}
|
---|
155 | Return the tangent of \var{x} radians.
|
---|
156 | \end{funcdesc}
|
---|
157 |
|
---|
158 | Angular conversion:
|
---|
159 |
|
---|
160 | \begin{funcdesc}{degrees}{x}
|
---|
161 | Converts angle \var{x} from radians to degrees.
|
---|
162 | \end{funcdesc}
|
---|
163 |
|
---|
164 | \begin{funcdesc}{radians}{x}
|
---|
165 | Converts angle \var{x} from degrees to radians.
|
---|
166 | \end{funcdesc}
|
---|
167 |
|
---|
168 | Hyperbolic functions:
|
---|
169 |
|
---|
170 | \begin{funcdesc}{cosh}{x}
|
---|
171 | Return the hyperbolic cosine of \var{x}.
|
---|
172 | \end{funcdesc}
|
---|
173 |
|
---|
174 | \begin{funcdesc}{sinh}{x}
|
---|
175 | Return the hyperbolic sine of \var{x}.
|
---|
176 | \end{funcdesc}
|
---|
177 |
|
---|
178 | \begin{funcdesc}{tanh}{x}
|
---|
179 | Return the hyperbolic tangent of \var{x}.
|
---|
180 | \end{funcdesc}
|
---|
181 |
|
---|
182 | The module also defines two mathematical constants:
|
---|
183 |
|
---|
184 | \begin{datadesc}{pi}
|
---|
185 | The mathematical constant \emph{pi}.
|
---|
186 | \end{datadesc}
|
---|
187 |
|
---|
188 | \begin{datadesc}{e}
|
---|
189 | The 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}
|
---|