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 |
Tim Peters | 66bb6e6 | 2004-07-24 23:00:24 +0000 | [diff] [blame] | 21 | when explicitly noted otherwise, all return values are floats. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 22 | |
Tim Peters | 66bb6e6 | 2004-07-24 23:00:24 +0000 | [diff] [blame] | 23 | Number-theoretic and representation functions: |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 24 | |
| 25 | \begin{funcdesc}{ceil}{x} |
Tim Peters | 66bb6e6 | 2004-07-24 23:00:24 +0000 | [diff] [blame] | 26 | Return the ceiling of \var{x} as a float, the smallest integer value |
| 27 | greater than or equal to \var{x}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 28 | \end{funcdesc} |
| 29 | |
| 30 | \begin{funcdesc}{fabs}{x} |
Tim Peters | a1af767 | 2003-04-28 21:32:03 +0000 | [diff] [blame] | 31 | Return the absolute value of \var{x}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 32 | \end{funcdesc} |
| 33 | |
| 34 | \begin{funcdesc}{floor}{x} |
Tim Peters | 66bb6e6 | 2004-07-24 23:00:24 +0000 | [diff] [blame] | 35 | Return the floor of \var{x} as a float, the largest integer value |
| 36 | less than or equal to \var{x}. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 37 | \end{funcdesc} |
| 38 | |
| 39 | \begin{funcdesc}{fmod}{x, y} |
Tim Peters | 78fc0b5 | 2000-09-16 03:54:24 +0000 | [diff] [blame] | 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 |
Tim Peters | 66bb6e6 | 2004-07-24 23:00:24 +0000 | [diff] [blame] | 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. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 55 | \end{funcdesc} |
| 56 | |
| 57 | \begin{funcdesc}{frexp}{x} |
Fred Drake | fcc95a4 | 2000-07-03 06:38:17 +0000 | [diff] [blame] | 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 |
Tim Peters | 66bb6e6 | 2004-07-24 23:00:24 +0000 | [diff] [blame] | 60 | integer such that \code{\var{x} == \var{m} * 2**\var{e}} exactly. |
Fred Drake | fcc95a4 | 2000-07-03 06:38:17 +0000 | [diff] [blame] | 61 | If \var{x} is zero, returns \code{(0.0, 0)}, otherwise |
Tim Peters | 66bb6e6 | 2004-07-24 23:00:24 +0000 | [diff] [blame] | 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. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 64 | \end{funcdesc} |
| 65 | |
| 66 | \begin{funcdesc}{ldexp}{x, i} |
Tim Peters | 66bb6e6 | 2004-07-24 23:00:24 +0000 | [diff] [blame] | 67 | Return \code{\var{x} * (2**\var{i})}. This is essentially the inverse of |
| 68 | function \function{frexp()}. |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 69 | \end{funcdesc} |
| 70 | |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 71 | \begin{funcdesc}{modf}{x} |
| 72 | Return the fractional and integer parts of \var{x}. Both results |
Tim Peters | 66bb6e6 | 2004-07-24 23:00:24 +0000 | [diff] [blame] | 73 | carry the sign of \var{x}, and both are floats. |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 74 | \end{funcdesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 75 | |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 76 | Note that \function{frexp()} and \function{modf()} have a different |
Fred Drake | 69fa563 | 1999-04-21 16:29:57 +0000 | [diff] [blame] | 77 | call/return pattern than their C equivalents: they take a single |
Fred Drake | 7c418ed | 1998-01-22 17:37:50 +0000 | [diff] [blame] | 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). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 81 | |
Tim Peters | 66bb6e6 | 2004-07-24 23:00:24 +0000 | [diff] [blame] | 82 | Power and logarithmic functions: |
| 83 | |
| 84 | \begin{funcdesc}{exp}{x} |
| 85 | Return \code{e**\var{x}}. |
| 86 | \end{funcdesc} |
| 87 | |
| 88 | \begin{funcdesc}{log}{x\optional{, base}} |
| 89 | Return the logarithm of \var{x} to the given \var{base}. |
| 90 | If the \var{base} is not specified, return the natural logarithm of \var{x} |
| 91 | (that is, the logarithm to base \emph{e}). |
| 92 | \versionchanged[\var{base} argument added]{2.3} |
| 93 | \end{funcdesc} |
| 94 | |
| 95 | \begin{funcdesc}{log10}{x} |
| 96 | Return the base-10 logarithm of \var{x}. |
| 97 | \end{funcdesc} |
| 98 | |
| 99 | \begin{funcdesc}{pow}{x, y} |
| 100 | Return \code{\var{x}**\var{y}}. |
| 101 | \end{funcdesc} |
| 102 | |
| 103 | \begin{funcdesc}{sqrt}{x} |
| 104 | Return the square root of \var{x}. |
| 105 | \end{funcdesc} |
| 106 | |
| 107 | Trigonometric functions: |
| 108 | |
| 109 | \begin{funcdesc}{acos}{x} |
| 110 | Return the arc cosine of \var{x}, in radians. |
| 111 | \end{funcdesc} |
| 112 | |
| 113 | \begin{funcdesc}{asin}{x} |
| 114 | Return the arc sine of \var{x}, in radians. |
| 115 | \end{funcdesc} |
| 116 | |
| 117 | \begin{funcdesc}{atan}{x} |
| 118 | Return the arc tangent of \var{x}, in radians. |
| 119 | \end{funcdesc} |
| 120 | |
| 121 | \begin{funcdesc}{atan2}{y, x} |
| 122 | Return \code{atan(\var{y} / \var{x})}, in radians. |
| 123 | The result is between \code{-pi} and \code{pi}. |
| 124 | The vector in the plane from the origin to point \code{(\var{x}, \var{y})} |
| 125 | makes this angle with the positive X axis. |
| 126 | The point of \function{atan2()} is that the signs of both inputs are |
| 127 | known to it, so it can compute the correct quadrant for the angle. |
| 128 | For example, \code{atan(1}) and \code{atan2(1, 1)} are both \code{pi/4}, |
| 129 | but \code{atan2(-1, -1)} is \code{-3*pi/4}. |
| 130 | \end{funcdesc} |
| 131 | |
| 132 | \begin{funcdesc}{cos}{x} |
| 133 | Return the cosine of \var{x} radians. |
| 134 | \end{funcdesc} |
| 135 | |
| 136 | \begin{funcdesc}{hypot}{x, y} |
| 137 | Return the Euclidean norm, \code{sqrt(\var{x}*\var{x} + \var{y}*\var{y})}. |
| 138 | This is the length of the vector from the origin to point |
| 139 | \code{(\var{x}, \var{y})}. |
| 140 | \end{funcdesc} |
| 141 | |
| 142 | \begin{funcdesc}{sin}{x} |
| 143 | Return the sine of \var{x} radians. |
| 144 | \end{funcdesc} |
| 145 | |
| 146 | \begin{funcdesc}{tan}{x} |
| 147 | Return the tangent of \var{x} radians. |
| 148 | \end{funcdesc} |
| 149 | |
| 150 | Angular conversion: |
| 151 | |
| 152 | \begin{funcdesc}{degrees}{x} |
| 153 | Converts angle \var{x} from radians to degrees. |
| 154 | \end{funcdesc} |
| 155 | |
| 156 | \begin{funcdesc}{radians}{x} |
| 157 | Converts angle \var{x} from degrees to radians. |
| 158 | \end{funcdesc} |
| 159 | |
| 160 | Hyerbolic functions: |
| 161 | |
| 162 | \begin{funcdesc}{cosh}{x} |
| 163 | Return the hyperbolic cosine of \var{x}. |
| 164 | \end{funcdesc} |
| 165 | |
| 166 | \begin{funcdesc}{sinh}{x} |
| 167 | Return the hyperbolic sine of \var{x}. |
| 168 | \end{funcdesc} |
| 169 | |
| 170 | \begin{funcdesc}{tanh}{x} |
| 171 | Return the hyperbolic tangent of \var{x}. |
| 172 | \end{funcdesc} |
| 173 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 174 | The module also defines two mathematical constants: |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 175 | |
Fred Drake | b55e07f | 1997-09-30 21:59:27 +0000 | [diff] [blame] | 176 | \begin{datadesc}{pi} |
| 177 | The mathematical constant \emph{pi}. |
| 178 | \end{datadesc} |
| 179 | |
| 180 | \begin{datadesc}{e} |
| 181 | The mathematical constant \emph{e}. |
| 182 | \end{datadesc} |
| 183 | |
Skip Montanaro | 5118341 | 2003-04-26 02:59:00 +0000 | [diff] [blame] | 184 | \begin{notice} |
Tim Peters | 965697f | 2003-04-26 15:11:08 +0000 | [diff] [blame] | 185 | The \module{math} module consists mostly of thin wrappers around |
| 186 | the platform C math library functions. Behavior in exceptional cases is |
| 187 | loosely specified by the C standards, and Python inherits much of its |
| 188 | math-function error-reporting behavior from the platform C |
| 189 | implementation. As a result, |
| 190 | the specific exceptions raised in error cases (and even whether some |
Skip Montanaro | 5118341 | 2003-04-26 02:59:00 +0000 | [diff] [blame] | 191 | arguments are considered to be exceptional at all) are not defined in any |
| 192 | useful cross-platform or cross-release way. For example, whether |
| 193 | \code{math.log(0)} returns \code{-Inf} or raises \exception{ValueError} or |
Tim Peters | 965697f | 2003-04-26 15:11:08 +0000 | [diff] [blame] | 194 | \exception{OverflowError} isn't defined, and in |
| 195 | cases where \code{math.log(0)} raises \exception{OverflowError}, |
| 196 | \code{math.log(0L)} may raise \exception{ValueError} instead. |
Skip Montanaro | 5118341 | 2003-04-26 02:59:00 +0000 | [diff] [blame] | 197 | \end{notice} |
| 198 | |
Fred Drake | 2950b2d | 1997-10-13 22:06:17 +0000 | [diff] [blame] | 199 | \begin{seealso} |
| 200 | \seemodule{cmath}{Complex number versions of many of these functions.} |
| 201 | \end{seealso} |