blob: f013e23a114ec5c9974ba3717e64940a1ac6ab2c [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
20The following functions provided by this module:
Fred Drakeb55e07f1997-09-30 21:59:27 +000021
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022\begin{funcdesc}{acos}{x}
Fred Drakeb55e07f1997-09-30 21:59:27 +000023Return the arc cosine of \var{x}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000024\end{funcdesc}
Fred Drakeb55e07f1997-09-30 21:59:27 +000025
26\begin{funcdesc}{asin}{x}
27Return the arc sine of \var{x}.
28\end{funcdesc}
29
30\begin{funcdesc}{atan}{x}
31Return the arc tangent of \var{x}.
32\end{funcdesc}
33
Fred Drake64583d31998-12-08 16:10:44 +000034\begin{funcdesc}{atan2}{y, x}
35Return \code{atan(\var{y} / \var{x})}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000036\end{funcdesc}
37
38\begin{funcdesc}{ceil}{x}
Fred Drake2b6d7bc2000-12-18 13:50:24 +000039Return the ceiling of \var{x} as a float.
Fred Drakeb55e07f1997-09-30 21:59:27 +000040\end{funcdesc}
41
42\begin{funcdesc}{cos}{x}
43Return the cosine of \var{x}.
44\end{funcdesc}
45
46\begin{funcdesc}{cosh}{x}
47Return the hyperbolic cosine of \var{x}.
48\end{funcdesc}
49
Raymond Hettingerc045b492002-05-13 03:52:47 +000050\begin{funcdesc}{degrees}{x}
51Converts angle \var{x} from radians to degrees.
52\end{funcdesc}
53
Fred Drakeb55e07f1997-09-30 21:59:27 +000054\begin{funcdesc}{exp}{x}
Guido van Rossumf259efe1997-11-25 01:00:40 +000055Return \code{e**\var{x}}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000056\end{funcdesc}
57
58\begin{funcdesc}{fabs}{x}
Fred Drake2b6d7bc2000-12-18 13:50:24 +000059Return the absolute value of the floating point number \var{x}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000060\end{funcdesc}
61
62\begin{funcdesc}{floor}{x}
Fred Drake2b6d7bc2000-12-18 13:50:24 +000063Return the floor of \var{x} as a float.
Fred Drakeb55e07f1997-09-30 21:59:27 +000064\end{funcdesc}
65
66\begin{funcdesc}{fmod}{x, y}
Tim Peters78fc0b52000-09-16 03:54:24 +000067Return \code{fmod(\var{x}, \var{y})}, as defined by the platform C library.
68Note that the Python expression \code{\var{x} \%\ \var{y}} may not return
69the same result.
Fred Drakeb55e07f1997-09-30 21:59:27 +000070\end{funcdesc}
71
72\begin{funcdesc}{frexp}{x}
Fred Drakefcc95a42000-07-03 06:38:17 +000073% Blessed by Tim.
74Return the mantissa and exponent of \var{x} as the pair
75\code{(\var{m}, \var{e})}. \var{m} is a float and \var{e} is an
76integer such that \code{\var{x} == \var{m} * 2**\var{e}}.
77If \var{x} is zero, returns \code{(0.0, 0)}, otherwise
78\code{0.5 <= abs(\var{m}) < 1}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000079\end{funcdesc}
80
81\begin{funcdesc}{hypot}{x, y}
Fred Draked327a8d1998-01-09 21:26:51 +000082Return the Euclidean distance, \code{sqrt(\var{x}*\var{x} + \var{y}*\var{y})}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000083\end{funcdesc}
84
85\begin{funcdesc}{ldexp}{x, i}
Guido van Rossumf259efe1997-11-25 01:00:40 +000086Return \code{\var{x} * (2**\var{i})}.
Fred Drakeb55e07f1997-09-30 21:59:27 +000087\end{funcdesc}
88
Fred Drake7c418ed1998-01-22 17:37:50 +000089\begin{funcdesc}{log}{x}
90Return the natural logarithm of \var{x}.
91\end{funcdesc}
92
93\begin{funcdesc}{log10}{x}
94Return the base-10 logarithm of \var{x}.
95\end{funcdesc}
96
Fred Drakeb55e07f1997-09-30 21:59:27 +000097\begin{funcdesc}{modf}{x}
98Return the fractional and integer parts of \var{x}. Both results
Fred Drake2b6d7bc2000-12-18 13:50:24 +000099carry the sign of \var{x}. The integer part is returned as a float.
Fred Drakeb55e07f1997-09-30 21:59:27 +0000100\end{funcdesc}
101
102\begin{funcdesc}{pow}{x, y}
Guido van Rossumf259efe1997-11-25 01:00:40 +0000103Return \code{\var{x}**\var{y}}.
Fred Drakeb55e07f1997-09-30 21:59:27 +0000104\end{funcdesc}
105
Raymond Hettingerc045b492002-05-13 03:52:47 +0000106\begin{funcdesc}{radians}{x}
107Converts angle \var{x} from degrees to radians.
108\end{funcdesc}
109
Fred Drakeb55e07f1997-09-30 21:59:27 +0000110\begin{funcdesc}{sin}{x}
111Return the sine of \var{x}.
112\end{funcdesc}
113
114\begin{funcdesc}{sinh}{x}
115Return the hyperbolic sine of \var{x}.
116\end{funcdesc}
117
118\begin{funcdesc}{sqrt}{x}
119Return the square root of \var{x}.
120\end{funcdesc}
121
122\begin{funcdesc}{tan}{x}
123Return the tangent of \var{x}.
124\end{funcdesc}
125
126\begin{funcdesc}{tanh}{x}
127Return the hyperbolic tangent of \var{x}.
128\end{funcdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000129
Fred Drake7c418ed1998-01-22 17:37:50 +0000130Note that \function{frexp()} and \function{modf()} have a different
Fred Drake69fa5631999-04-21 16:29:57 +0000131call/return pattern than their C equivalents: they take a single
Fred Drake7c418ed1998-01-22 17:37:50 +0000132argument and return a pair of values, rather than returning their
133second return value through an `output parameter' (there is no such
134thing in Python).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000135
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000136The module also defines two mathematical constants:
Guido van Rossume47da0a1997-07-17 16:34:52 +0000137
Fred Drakeb55e07f1997-09-30 21:59:27 +0000138\begin{datadesc}{pi}
139The mathematical constant \emph{pi}.
140\end{datadesc}
141
142\begin{datadesc}{e}
143The mathematical constant \emph{e}.
144\end{datadesc}
145
Fred Drake2950b2d1997-10-13 22:06:17 +0000146\begin{seealso}
147 \seemodule{cmath}{Complex number versions of many of these functions.}
148\end{seealso}