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 | |
Tim Peters | a1af767 | 2003-04-28 21:32:03 +0000 | [diff] [blame] | 20 | The following functions are provided by this module. Except |
| 21 | when explicitly noted otherwise, all return values are floats: |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 23 | \begin{funcdesc}{acos}{x} |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 24 | Return the arc cosine of \var{x}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 25 | \end{funcdesc} |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 26 | |
| 27 | \begin{funcdesc}{asin}{x} |
| 28 | Return the arc sine of \var{x}. |
| 29 | \end{funcdesc} |
| 30 | |
| 31 | \begin{funcdesc}{atan}{x} |
| 32 | Return the arc tangent of \var{x}. |
| 33 | \end{funcdesc} |
| 34 | |
Fred Drake | 64583d3 | 1998-12-08 16:10:44 +0000 | [diff] [blame] | 35 | \begin{funcdesc}{atan2}{y, x} |
| 36 | Return \code{atan(\var{y} / \var{x})}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 37 | \end{funcdesc} |
| 38 | |
| 39 | \begin{funcdesc}{ceil}{x} |
Fred Drake | 2b6d7bc | 2000-12-18 13:50:24 +0000 | [diff] [blame] | 40 | Return the ceiling of \var{x} as a float. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 41 | \end{funcdesc} |
| 42 | |
| 43 | \begin{funcdesc}{cos}{x} |
| 44 | Return the cosine of \var{x}. |
| 45 | \end{funcdesc} |
| 46 | |
| 47 | \begin{funcdesc}{cosh}{x} |
| 48 | Return the hyperbolic cosine of \var{x}. |
| 49 | \end{funcdesc} |
| 50 | |
Raymond Hettinger | c045b49 | 2002-05-13 03:52:47 +0000 | [diff] [blame] | 51 | \begin{funcdesc}{degrees}{x} |
| 52 | Converts angle \var{x} from radians to degrees. |
| 53 | \end{funcdesc} |
| 54 | |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 55 | \begin{funcdesc}{exp}{x} |
Guido van Rossum | f259efe | 1997-11-25 01:00:40 +0000 | [diff] [blame] | 56 | Return \code{e**\var{x}}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 57 | \end{funcdesc} |
| 58 | |
| 59 | \begin{funcdesc}{fabs}{x} |
Tim Peters | a1af767 | 2003-04-28 21:32:03 +0000 | [diff] [blame] | 60 | Return the absolute value of \var{x}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 61 | \end{funcdesc} |
| 62 | |
| 63 | \begin{funcdesc}{floor}{x} |
Fred Drake | 2b6d7bc | 2000-12-18 13:50:24 +0000 | [diff] [blame] | 64 | Return the floor of \var{x} as a float. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 65 | \end{funcdesc} |
| 66 | |
| 67 | \begin{funcdesc}{fmod}{x, y} |
Tim Peters | 78fc0b5 | 2000-09-16 03:54:24 +0000 | [diff] [blame] | 68 | Return \code{fmod(\var{x}, \var{y})}, as defined by the platform C library. |
| 69 | Note that the Python expression \code{\var{x} \%\ \var{y}} may not return |
| 70 | the same result. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 71 | \end{funcdesc} |
| 72 | |
| 73 | \begin{funcdesc}{frexp}{x} |
Fred Drake | fcc95a4 | 2000-07-03 06:38:17 +0000 | [diff] [blame] | 74 | % Blessed by Tim. |
| 75 | Return 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 |
| 77 | integer such that \code{\var{x} == \var{m} * 2**\var{e}}. |
| 78 | If \var{x} is zero, returns \code{(0.0, 0)}, otherwise |
| 79 | \code{0.5 <= abs(\var{m}) < 1}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 80 | \end{funcdesc} |
| 81 | |
| 82 | \begin{funcdesc}{hypot}{x, y} |
Fred Drake | d327a8d | 1998-01-09 21:26:51 +0000 | [diff] [blame] | 83 | 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] | 84 | \end{funcdesc} |
| 85 | |
| 86 | \begin{funcdesc}{ldexp}{x, i} |
Guido van Rossum | f259efe | 1997-11-25 01:00:40 +0000 | [diff] [blame] | 87 | Return \code{\var{x} * (2**\var{i})}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 88 | \end{funcdesc} |
| 89 | |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 90 | \begin{funcdesc}{log}{x\optional{, base}} |
Neal Norwitz | 212b43f | 2002-12-17 01:24:11 +0000 | [diff] [blame] | 91 | Returns the logarithm of \var{x} to the given \var{base}. |
| 92 | If the \var{base} is not specified, returns the natural logarithm of \var{x}. |
Raymond Hettinger | 866964c | 2002-12-14 19:51:34 +0000 | [diff] [blame] | 93 | \versionchanged[\var{base} argument added]{2.3} |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 94 | \end{funcdesc} |
| 95 | |
| 96 | \begin{funcdesc}{log10}{x} |
| 97 | Return the base-10 logarithm of \var{x}. |
| 98 | \end{funcdesc} |
| 99 | |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 100 | \begin{funcdesc}{modf}{x} |
| 101 | Return the fractional and integer parts of \var{x}. Both results |
Fred Drake | 2b6d7bc | 2000-12-18 13:50:24 +0000 | [diff] [blame] | 102 | carry the sign of \var{x}. The integer part is returned as a float. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 103 | \end{funcdesc} |
| 104 | |
| 105 | \begin{funcdesc}{pow}{x, y} |
Guido van Rossum | f259efe | 1997-11-25 01:00:40 +0000 | [diff] [blame] | 106 | Return \code{\var{x}**\var{y}}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 107 | \end{funcdesc} |
| 108 | |
Raymond Hettinger | c045b49 | 2002-05-13 03:52:47 +0000 | [diff] [blame] | 109 | \begin{funcdesc}{radians}{x} |
| 110 | Converts angle \var{x} from degrees to radians. |
| 111 | \end{funcdesc} |
| 112 | |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 113 | \begin{funcdesc}{sin}{x} |
| 114 | Return the sine of \var{x}. |
| 115 | \end{funcdesc} |
| 116 | |
| 117 | \begin{funcdesc}{sinh}{x} |
| 118 | Return the hyperbolic sine of \var{x}. |
| 119 | \end{funcdesc} |
| 120 | |
| 121 | \begin{funcdesc}{sqrt}{x} |
| 122 | Return the square root of \var{x}. |
| 123 | \end{funcdesc} |
| 124 | |
| 125 | \begin{funcdesc}{tan}{x} |
| 126 | Return the tangent of \var{x}. |
| 127 | \end{funcdesc} |
| 128 | |
| 129 | \begin{funcdesc}{tanh}{x} |
| 130 | Return the hyperbolic tangent of \var{x}. |
| 131 | \end{funcdesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 132 | |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 133 | Note that \function{frexp()} and \function{modf()} have a different |
Fred Drake | 69fa563 | 1999-04-21 16:29:57 +0000 | [diff] [blame] | 134 | call/return pattern than their C equivalents: they take a single |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 135 | argument and return a pair of values, rather than returning their |
| 136 | second return value through an `output parameter' (there is no such |
| 137 | thing in Python). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 139 | The module also defines two mathematical constants: |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 140 | |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 141 | \begin{datadesc}{pi} |
| 142 | The mathematical constant \emph{pi}. |
| 143 | \end{datadesc} |
| 144 | |
| 145 | \begin{datadesc}{e} |
| 146 | The mathematical constant \emph{e}. |
| 147 | \end{datadesc} |
| 148 | |
Skip Montanaro | 5118341 | 2003-04-26 02:59:00 +0000 | [diff] [blame] | 149 | \begin{notice} |
Tim Peters | 965697f | 2003-04-26 15:11:08 +0000 | [diff] [blame] | 150 | 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 Montanaro | 5118341 | 2003-04-26 02:59:00 +0000 | [diff] [blame] | 156 | 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 Peters | 965697f | 2003-04-26 15:11:08 +0000 | [diff] [blame] | 159 | \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 Montanaro | 5118341 | 2003-04-26 02:59:00 +0000 | [diff] [blame] | 162 | \end{notice} |
| 163 | |
Fred Drake | 2950b2d | 1997-10-13 22:06:17 +0000 | [diff] [blame] | 164 | \begin{seealso} |
| 165 | \seemodule{cmath}{Complex number versions of many of these functions.} |
| 166 | \end{seealso} |