Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`math` --- Mathematical functions |
| 3 | ====================================== |
| 4 | |
| 5 | .. module:: math |
| 6 | :synopsis: Mathematical functions (sin() etc.). |
| 7 | |
| 8 | |
| 9 | This module is always available. It provides access to the mathematical |
| 10 | functions defined by the C standard. |
| 11 | |
| 12 | These functions cannot be used with complex numbers; use the functions of the |
| 13 | same name from the :mod:`cmath` module if you require support for complex |
| 14 | numbers. The distinction between functions which support complex numbers and |
| 15 | those which don't is made since most users do not want to learn quite as much |
| 16 | mathematics as required to understand complex numbers. Receiving an exception |
| 17 | instead of a complex result allows earlier detection of the unexpected complex |
| 18 | number used as a parameter, so that the programmer can determine how and why it |
| 19 | was generated in the first place. |
| 20 | |
| 21 | The following functions are provided by this module. Except when explicitly |
| 22 | noted otherwise, all return values are floats. |
| 23 | |
| 24 | Number-theoretic and representation functions: |
| 25 | |
| 26 | |
| 27 | .. function:: ceil(x) |
| 28 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 29 | Return the ceiling of *x* as a float, the smallest integer value greater than |
| 30 | or equal to *x*. If *x* is not a float, delegates to ``x.__ceil__()``, which |
| 31 | should return an :class:`Integral` value. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 32 | |
| 33 | |
| 34 | .. function:: fabs(x) |
| 35 | |
| 36 | Return the absolute value of *x*. |
| 37 | |
| 38 | |
| 39 | .. function:: floor(x) |
| 40 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 41 | Return the floor of *x* as a float, the largest integer value less than or |
| 42 | equal to *x*. If *x* is not a float, delegates to ``x.__floor__()``, which |
| 43 | should return an :class:`Integral` value. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | .. function:: fmod(x, y) |
| 47 | |
| 48 | Return ``fmod(x, y)``, as defined by the platform C library. Note that the |
| 49 | Python expression ``x % y`` may not return the same result. The intent of the C |
| 50 | standard is that ``fmod(x, y)`` be exactly (mathematically; to infinite |
| 51 | precision) equal to ``x - n*y`` for some integer *n* such that the result has |
| 52 | the same sign as *x* and magnitude less than ``abs(y)``. Python's ``x % y`` |
| 53 | returns a result with the sign of *y* instead, and may not be exactly computable |
| 54 | for float arguments. For example, ``fmod(-1e-100, 1e100)`` is ``-1e-100``, but |
| 55 | the result of Python's ``-1e-100 % 1e100`` is ``1e100-1e-100``, which cannot be |
| 56 | represented exactly as a float, and rounds to the surprising ``1e100``. For |
| 57 | this reason, function :func:`fmod` is generally preferred when working with |
| 58 | floats, while Python's ``x % y`` is preferred when working with integers. |
| 59 | |
| 60 | |
| 61 | .. function:: frexp(x) |
| 62 | |
| 63 | Return the mantissa and exponent of *x* as the pair ``(m, e)``. *m* is a float |
| 64 | and *e* is an integer such that ``x == m * 2**e`` exactly. If *x* is zero, |
| 65 | returns ``(0.0, 0)``, otherwise ``0.5 <= abs(m) < 1``. This is used to "pick |
| 66 | apart" the internal representation of a float in a portable way. |
| 67 | |
| 68 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame^] | 69 | .. function:: isinf(x) |
| 70 | |
| 71 | Checks if the float *x* is positive or negative infinite. |
| 72 | |
| 73 | ..versionadded:: 2.6 |
| 74 | |
| 75 | |
| 76 | .. function:: isnan(x) |
| 77 | |
| 78 | Checks if the float *x* is a NaN (not a number). NaNs are part of the |
| 79 | IEEE 754 standards. Operation like but not limited to ``inf * 0``, |
| 80 | ``inf / inf`` or any operation involving a NaN, e.g. ``nan * 1``, return |
| 81 | a NaN. |
| 82 | |
| 83 | ..versionadded:: 2.6 |
| 84 | |
| 85 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 86 | .. function:: ldexp(x, i) |
| 87 | |
| 88 | Return ``x * (2**i)``. This is essentially the inverse of function |
| 89 | :func:`frexp`. |
| 90 | |
| 91 | |
| 92 | .. function:: modf(x) |
| 93 | |
| 94 | Return the fractional and integer parts of *x*. Both results carry the sign of |
| 95 | *x*, and both are floats. |
| 96 | |
| 97 | Note that :func:`frexp` and :func:`modf` have a different call/return pattern |
| 98 | than their C equivalents: they take a single argument and return a pair of |
| 99 | values, rather than returning their second return value through an 'output |
| 100 | parameter' (there is no such thing in Python). |
| 101 | |
| 102 | For the :func:`ceil`, :func:`floor`, and :func:`modf` functions, note that *all* |
| 103 | floating-point numbers of sufficiently large magnitude are exact integers. |
| 104 | Python floats typically carry no more than 53 bits of precision (the same as the |
| 105 | platform C double type), in which case any float *x* with ``abs(x) >= 2**52`` |
| 106 | necessarily has no fractional bits. |
| 107 | |
| 108 | Power and logarithmic functions: |
| 109 | |
| 110 | |
| 111 | .. function:: exp(x) |
| 112 | |
| 113 | Return ``e**x``. |
| 114 | |
| 115 | |
| 116 | .. function:: log(x[, base]) |
| 117 | |
| 118 | Return the logarithm of *x* to the given *base*. If the *base* is not specified, |
| 119 | return the natural logarithm of *x* (that is, the logarithm to base *e*). |
| 120 | |
| 121 | .. versionchanged:: 2.3 |
| 122 | *base* argument added. |
| 123 | |
| 124 | |
| 125 | .. function:: log10(x) |
| 126 | |
| 127 | Return the base-10 logarithm of *x*. |
| 128 | |
| 129 | |
| 130 | .. function:: pow(x, y) |
| 131 | |
| 132 | Return ``x**y``. |
| 133 | |
| 134 | |
| 135 | .. function:: sqrt(x) |
| 136 | |
| 137 | Return the square root of *x*. |
| 138 | |
| 139 | Trigonometric functions: |
| 140 | |
| 141 | |
| 142 | .. function:: acos(x) |
| 143 | |
| 144 | Return the arc cosine of *x*, in radians. |
| 145 | |
| 146 | |
| 147 | .. function:: asin(x) |
| 148 | |
| 149 | Return the arc sine of *x*, in radians. |
| 150 | |
| 151 | |
| 152 | .. function:: atan(x) |
| 153 | |
| 154 | Return the arc tangent of *x*, in radians. |
| 155 | |
| 156 | |
| 157 | .. function:: atan2(y, x) |
| 158 | |
| 159 | Return ``atan(y / x)``, in radians. The result is between ``-pi`` and ``pi``. |
| 160 | The vector in the plane from the origin to point ``(x, y)`` makes this angle |
| 161 | with the positive X axis. The point of :func:`atan2` is that the signs of both |
| 162 | inputs are known to it, so it can compute the correct quadrant for the angle. |
| 163 | For example, ``atan(1``) and ``atan2(1, 1)`` are both ``pi/4``, but ``atan2(-1, |
| 164 | -1)`` is ``-3*pi/4``. |
| 165 | |
| 166 | |
| 167 | .. function:: cos(x) |
| 168 | |
| 169 | Return the cosine of *x* radians. |
| 170 | |
| 171 | |
| 172 | .. function:: hypot(x, y) |
| 173 | |
| 174 | Return the Euclidean norm, ``sqrt(x*x + y*y)``. This is the length of the vector |
| 175 | from the origin to point ``(x, y)``. |
| 176 | |
| 177 | |
| 178 | .. function:: sin(x) |
| 179 | |
| 180 | Return the sine of *x* radians. |
| 181 | |
| 182 | |
| 183 | .. function:: tan(x) |
| 184 | |
| 185 | Return the tangent of *x* radians. |
| 186 | |
| 187 | Angular conversion: |
| 188 | |
| 189 | |
| 190 | .. function:: degrees(x) |
| 191 | |
| 192 | Converts angle *x* from radians to degrees. |
| 193 | |
| 194 | |
| 195 | .. function:: radians(x) |
| 196 | |
| 197 | Converts angle *x* from degrees to radians. |
| 198 | |
| 199 | Hyperbolic functions: |
| 200 | |
| 201 | |
| 202 | .. function:: cosh(x) |
| 203 | |
| 204 | Return the hyperbolic cosine of *x*. |
| 205 | |
| 206 | |
| 207 | .. function:: sinh(x) |
| 208 | |
| 209 | Return the hyperbolic sine of *x*. |
| 210 | |
| 211 | |
| 212 | .. function:: tanh(x) |
| 213 | |
| 214 | Return the hyperbolic tangent of *x*. |
| 215 | |
| 216 | The module also defines two mathematical constants: |
| 217 | |
| 218 | |
| 219 | .. data:: pi |
| 220 | |
| 221 | The mathematical constant *pi*. |
| 222 | |
| 223 | |
| 224 | .. data:: e |
| 225 | |
| 226 | The mathematical constant *e*. |
| 227 | |
| 228 | .. note:: |
| 229 | |
| 230 | The :mod:`math` module consists mostly of thin wrappers around the platform C |
| 231 | math library functions. Behavior in exceptional cases is loosely specified |
| 232 | by the C standards, and Python inherits much of its math-function |
| 233 | error-reporting behavior from the platform C implementation. As a result, |
| 234 | the specific exceptions raised in error cases (and even whether some |
| 235 | arguments are considered to be exceptional at all) are not defined in any |
| 236 | useful cross-platform or cross-release way. For example, whether |
| 237 | ``math.log(0)`` returns ``-Inf`` or raises :exc:`ValueError` or |
| 238 | :exc:`OverflowError` isn't defined, and in cases where ``math.log(0)`` raises |
| 239 | :exc:`OverflowError`, ``math.log(0L)`` may raise :exc:`ValueError` instead. |
| 240 | |
| 241 | |
| 242 | .. seealso:: |
| 243 | |
| 244 | Module :mod:`cmath` |
| 245 | Complex number versions of many of these functions. |
| 246 | |