Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{math} --- |
Fred Drake | 69fa563 | 1999-04-21 16:29:57 +0000 | [diff] [blame] | 2 | Mathematical functions} |
| 3 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{math} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Mathematical functions (\function{sin()} etc.).} |
| 6 | |
Fred Drake | 69fa563 | 1999-04-21 16:29:57 +0000 | [diff] [blame] | 7 | This module is always available. It provides access to the |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 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 provided by this module: |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 22 | \begin{funcdesc}{acos}{x} |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 23 | Return the arc cosine of \var{x}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 24 | \end{funcdesc} |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 25 | |
| 26 | \begin{funcdesc}{asin}{x} |
| 27 | Return the arc sine of \var{x}. |
| 28 | \end{funcdesc} |
| 29 | |
| 30 | \begin{funcdesc}{atan}{x} |
| 31 | Return the arc tangent of \var{x}. |
| 32 | \end{funcdesc} |
| 33 | |
Fred Drake | 64583d3 | 1998-12-08 16:10:44 +0000 | [diff] [blame] | 34 | \begin{funcdesc}{atan2}{y, x} |
| 35 | Return \code{atan(\var{y} / \var{x})}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 36 | \end{funcdesc} |
| 37 | |
| 38 | \begin{funcdesc}{ceil}{x} |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 39 | Return the ceiling of \var{x} as a real. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 40 | \end{funcdesc} |
| 41 | |
| 42 | \begin{funcdesc}{cos}{x} |
| 43 | Return the cosine of \var{x}. |
| 44 | \end{funcdesc} |
| 45 | |
| 46 | \begin{funcdesc}{cosh}{x} |
| 47 | Return the hyperbolic cosine of \var{x}. |
| 48 | \end{funcdesc} |
| 49 | |
| 50 | \begin{funcdesc}{exp}{x} |
Guido van Rossum | f259efe | 1997-11-25 01:00:40 +0000 | [diff] [blame] | 51 | Return \code{e**\var{x}}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 52 | \end{funcdesc} |
| 53 | |
| 54 | \begin{funcdesc}{fabs}{x} |
| 55 | Return the absolute value of the real \var{x}. |
| 56 | \end{funcdesc} |
| 57 | |
| 58 | \begin{funcdesc}{floor}{x} |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 59 | Return the floor of \var{x} as a real. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 60 | \end{funcdesc} |
| 61 | |
| 62 | \begin{funcdesc}{fmod}{x, y} |
Fred Drake | d327a8d | 1998-01-09 21:26:51 +0000 | [diff] [blame] | 63 | Return \code{\var{x} \%\ \var{y}}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 64 | \end{funcdesc} |
| 65 | |
| 66 | \begin{funcdesc}{frexp}{x} |
Fred Drake | fcc95a4 | 2000-07-03 06:38:17 +0000 | [diff] [blame^] | 67 | % Blessed by Tim. |
| 68 | Return the mantissa and exponent of \var{x} as the pair |
| 69 | \code{(\var{m}, \var{e})}. \var{m} is a float and \var{e} is an |
| 70 | integer such that \code{\var{x} == \var{m} * 2**\var{e}}. |
| 71 | If \var{x} is zero, returns \code{(0.0, 0)}, otherwise |
| 72 | \code{0.5 <= abs(\var{m}) < 1}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 73 | \end{funcdesc} |
| 74 | |
| 75 | \begin{funcdesc}{hypot}{x, y} |
Fred Drake | d327a8d | 1998-01-09 21:26:51 +0000 | [diff] [blame] | 76 | Return the Euclidean distance, \code{sqrt(\var{x}*\var{x} + \var{y}*\var{y})}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 77 | \end{funcdesc} |
| 78 | |
| 79 | \begin{funcdesc}{ldexp}{x, i} |
Guido van Rossum | f259efe | 1997-11-25 01:00:40 +0000 | [diff] [blame] | 80 | Return \code{\var{x} * (2**\var{i})}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 81 | \end{funcdesc} |
| 82 | |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 83 | \begin{funcdesc}{log}{x} |
| 84 | Return the natural logarithm of \var{x}. |
| 85 | \end{funcdesc} |
| 86 | |
| 87 | \begin{funcdesc}{log10}{x} |
| 88 | Return the base-10 logarithm of \var{x}. |
| 89 | \end{funcdesc} |
| 90 | |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 91 | \begin{funcdesc}{modf}{x} |
| 92 | Return the fractional and integer parts of \var{x}. Both results |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 93 | carry the sign of \var{x}. The integer part is returned as a real. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 94 | \end{funcdesc} |
| 95 | |
| 96 | \begin{funcdesc}{pow}{x, y} |
Guido van Rossum | f259efe | 1997-11-25 01:00:40 +0000 | [diff] [blame] | 97 | Return \code{\var{x}**\var{y}}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 98 | \end{funcdesc} |
| 99 | |
Guido van Rossum | 71260b8 | 2000-05-11 18:19:42 +0000 | [diff] [blame] | 100 | \begin{funcdesc}{rint}{x, y} |
| 101 | Return the integer nearest to \var{x} as a real. |
Guido van Rossum | c9a5f34 | 2000-05-11 18:42:27 +0000 | [diff] [blame] | 102 | (Only available on platforms where this is in the standard C math library.) |
Guido van Rossum | 71260b8 | 2000-05-11 18:19:42 +0000 | [diff] [blame] | 103 | \end{funcdesc} |
| 104 | |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 105 | \begin{funcdesc}{sin}{x} |
| 106 | Return the sine of \var{x}. |
| 107 | \end{funcdesc} |
| 108 | |
| 109 | \begin{funcdesc}{sinh}{x} |
| 110 | Return the hyperbolic sine of \var{x}. |
| 111 | \end{funcdesc} |
| 112 | |
| 113 | \begin{funcdesc}{sqrt}{x} |
| 114 | Return the square root of \var{x}. |
| 115 | \end{funcdesc} |
| 116 | |
| 117 | \begin{funcdesc}{tan}{x} |
| 118 | Return the tangent of \var{x}. |
| 119 | \end{funcdesc} |
| 120 | |
| 121 | \begin{funcdesc}{tanh}{x} |
| 122 | Return the hyperbolic tangent of \var{x}. |
| 123 | \end{funcdesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 124 | |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 125 | Note that \function{frexp()} and \function{modf()} have a different |
Fred Drake | 69fa563 | 1999-04-21 16:29:57 +0000 | [diff] [blame] | 126 | call/return pattern than their C equivalents: they take a single |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 127 | argument and return a pair of values, rather than returning their |
| 128 | second return value through an `output parameter' (there is no such |
| 129 | thing in Python). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 130 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 131 | The module also defines two mathematical constants: |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 132 | |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 133 | \begin{datadesc}{pi} |
| 134 | The mathematical constant \emph{pi}. |
| 135 | \end{datadesc} |
| 136 | |
| 137 | \begin{datadesc}{e} |
| 138 | The mathematical constant \emph{e}. |
| 139 | \end{datadesc} |
| 140 | |
Fred Drake | 2950b2d | 1997-10-13 22:06:17 +0000 | [diff] [blame] | 141 | \begin{seealso} |
| 142 | \seemodule{cmath}{Complex number versions of many of these functions.} |
| 143 | \end{seealso} |