blob: a9cbc77d83c6c8ea80138e55341628c316fcb646 [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
21when explicitly noted otherwise, all return values are floats:
Fred Drakeb55e07f1997-09-30 21:59:27 +000022
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023\begin{funcdesc}{acos}{x}
Fred Drakeb55e07f1997-09-30 21:59:27 +000024Return the arc cosine of \var{x}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000025\end{funcdesc}
Fred Drakeb55e07f1997-09-30 21:59:27 +000026
27\begin{funcdesc}{asin}{x}
28Return the arc sine of \var{x}.
29\end{funcdesc}
30
31\begin{funcdesc}{atan}{x}
32Return the arc tangent of \var{x}.
33\end{funcdesc}
34
Fred Drake64583d31998-12-08 16:10:44 +000035\begin{funcdesc}{atan2}{y, x}
36Return \code{atan(\var{y} / \var{x})}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000037\end{funcdesc}
38
39\begin{funcdesc}{ceil}{x}
Fred Drake2b6d7bc2000-12-18 13:50:24 +000040Return the ceiling of \var{x} as a float.
Fred Drakeb55e07f1997-09-30 21:59:27 +000041\end{funcdesc}
42
43\begin{funcdesc}{cos}{x}
44Return the cosine of \var{x}.
45\end{funcdesc}
46
47\begin{funcdesc}{cosh}{x}
48Return the hyperbolic cosine of \var{x}.
49\end{funcdesc}
50
Raymond Hettingerc045b492002-05-13 03:52:47 +000051\begin{funcdesc}{degrees}{x}
52Converts angle \var{x} from radians to degrees.
53\end{funcdesc}
54
Fred Drakeb55e07f1997-09-30 21:59:27 +000055\begin{funcdesc}{exp}{x}
Guido van Rossumf259efe1997-11-25 01:00:40 +000056Return \code{e**\var{x}}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000057\end{funcdesc}
58
59\begin{funcdesc}{fabs}{x}
Tim Petersa1af7672003-04-28 21:32:03 +000060Return the absolute value of \var{x}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000061\end{funcdesc}
62
63\begin{funcdesc}{floor}{x}
Fred Drake2b6d7bc2000-12-18 13:50:24 +000064Return the floor of \var{x} as a float.
Fred Drakeb55e07f1997-09-30 21:59:27 +000065\end{funcdesc}
66
67\begin{funcdesc}{fmod}{x, y}
Tim Peters78fc0b52000-09-16 03:54:24 +000068Return \code{fmod(\var{x}, \var{y})}, as defined by the platform C library.
69Note that the Python expression \code{\var{x} \%\ \var{y}} may not return
70the same result.
Fred Drakeb55e07f1997-09-30 21:59:27 +000071\end{funcdesc}
72
73\begin{funcdesc}{frexp}{x}
Fred Drakefcc95a42000-07-03 06:38:17 +000074% Blessed by Tim.
75Return the mantissa and exponent of \var{x} as the pair
76\code{(\var{m}, \var{e})}. \var{m} is a float and \var{e} is an
77integer such that \code{\var{x} == \var{m} * 2**\var{e}}.
78If \var{x} is zero, returns \code{(0.0, 0)}, otherwise
79\code{0.5 <= abs(\var{m}) < 1}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000080\end{funcdesc}
81
82\begin{funcdesc}{hypot}{x, y}
Fred Draked327a8d1998-01-09 21:26:51 +000083Return the Euclidean distance, \code{sqrt(\var{x}*\var{x} + \var{y}*\var{y})}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000084\end{funcdesc}
85
86\begin{funcdesc}{ldexp}{x, i}
Guido van Rossumf259efe1997-11-25 01:00:40 +000087Return \code{\var{x} * (2**\var{i})}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000088\end{funcdesc}
89
Raymond Hettinger866964c2002-12-14 19:51:34 +000090\begin{funcdesc}{log}{x\optional{, base}}
Neal Norwitz212b43f2002-12-17 01:24:11 +000091Returns the logarithm of \var{x} to the given \var{base}.
92If the \var{base} is not specified, returns the natural logarithm of \var{x}.
Raymond Hettinger866964c2002-12-14 19:51:34 +000093\versionchanged[\var{base} argument added]{2.3}
Fred Drake7c418ed1998-01-22 17:37:50 +000094\end{funcdesc}
95
96\begin{funcdesc}{log10}{x}
97Return the base-10 logarithm of \var{x}.
98\end{funcdesc}
99
Fred Drakeb55e07f1997-09-30 21:59:27 +0000100\begin{funcdesc}{modf}{x}
101Return the fractional and integer parts of \var{x}. Both results
Fred Drake2b6d7bc2000-12-18 13:50:24 +0000102carry the sign of \var{x}. The integer part is returned as a float.
Fred Drakeb55e07f1997-09-30 21:59:27 +0000103\end{funcdesc}
104
105\begin{funcdesc}{pow}{x, y}
Guido van Rossumf259efe1997-11-25 01:00:40 +0000106Return \code{\var{x}**\var{y}}.
Fred Drakeb55e07f1997-09-30 21:59:27 +0000107\end{funcdesc}
108
Raymond Hettingerc045b492002-05-13 03:52:47 +0000109\begin{funcdesc}{radians}{x}
110Converts angle \var{x} from degrees to radians.
111\end{funcdesc}
112
Fred Drakeb55e07f1997-09-30 21:59:27 +0000113\begin{funcdesc}{sin}{x}
114Return the sine of \var{x}.
115\end{funcdesc}
116
117\begin{funcdesc}{sinh}{x}
118Return the hyperbolic sine of \var{x}.
119\end{funcdesc}
120
121\begin{funcdesc}{sqrt}{x}
122Return the square root of \var{x}.
123\end{funcdesc}
124
125\begin{funcdesc}{tan}{x}
126Return the tangent of \var{x}.
127\end{funcdesc}
128
129\begin{funcdesc}{tanh}{x}
130Return the hyperbolic tangent of \var{x}.
131\end{funcdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132
Fred Drake7c418ed1998-01-22 17:37:50 +0000133Note that \function{frexp()} and \function{modf()} have a different
Fred Drake69fa5631999-04-21 16:29:57 +0000134call/return pattern than their C equivalents: they take a single
Fred Drake7c418ed1998-01-22 17:37:50 +0000135argument and return a pair of values, rather than returning their
136second return value through an `output parameter' (there is no such
137thing in Python).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000138
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000139The module also defines two mathematical constants:
Guido van Rossume47da0a1997-07-17 16:34:52 +0000140
Fred Drakeb55e07f1997-09-30 21:59:27 +0000141\begin{datadesc}{pi}
142The mathematical constant \emph{pi}.
143\end{datadesc}
144
145\begin{datadesc}{e}
146The mathematical constant \emph{e}.
147\end{datadesc}
148
Skip Montanaro51183412003-04-26 02:59:00 +0000149\begin{notice}
Tim Peters965697f2003-04-26 15:11:08 +0000150 The \module{math} module consists mostly of thin wrappers around
151 the platform C math library functions. Behavior in exceptional cases is
152 loosely specified by the C standards, and Python inherits much of its
153 math-function error-reporting behavior from the platform C
154 implementation. As a result,
155 the specific exceptions raised in error cases (and even whether some
Skip Montanaro51183412003-04-26 02:59:00 +0000156 arguments are considered to be exceptional at all) are not defined in any
157 useful cross-platform or cross-release way. For example, whether
158 \code{math.log(0)} returns \code{-Inf} or raises \exception{ValueError} or
Tim Peters965697f2003-04-26 15:11:08 +0000159 \exception{OverflowError} isn't defined, and in
160 cases where \code{math.log(0)} raises \exception{OverflowError},
161 \code{math.log(0L)} may raise \exception{ValueError} instead.
Skip Montanaro51183412003-04-26 02:59:00 +0000162\end{notice}
163
Fred Drake2950b2d1997-10-13 22:06:17 +0000164\begin{seealso}
165 \seemodule{cmath}{Complex number versions of many of these functions.}
166\end{seealso}