Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`math` --- Mathematical functions |
| 2 | ====================================== |
| 3 | |
| 4 | .. module:: math |
| 5 | :synopsis: Mathematical functions (sin() etc.). |
| 6 | |
Łukasz Langa | 288234f | 2013-01-18 13:40:43 +0100 | [diff] [blame] | 7 | .. testsetup:: |
| 8 | |
| 9 | from math import fsum |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 11 | -------------- |
| 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | This module is always available. It provides access to the mathematical |
| 14 | functions defined by the C standard. |
| 15 | |
| 16 | These functions cannot be used with complex numbers; use the functions of the |
| 17 | same name from the :mod:`cmath` module if you require support for complex |
| 18 | numbers. The distinction between functions which support complex numbers and |
| 19 | those which don't is made since most users do not want to learn quite as much |
| 20 | mathematics as required to understand complex numbers. Receiving an exception |
| 21 | instead of a complex result allows earlier detection of the unexpected complex |
| 22 | number used as a parameter, so that the programmer can determine how and why it |
| 23 | was generated in the first place. |
| 24 | |
| 25 | The following functions are provided by this module. Except when explicitly |
| 26 | noted otherwise, all return values are floats. |
| 27 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 29 | Number-theoretic and representation functions |
| 30 | --------------------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
| 32 | .. function:: ceil(x) |
| 33 | |
Georg Brandl | 2a03373 | 2008-04-05 17:37:09 +0000 | [diff] [blame] | 34 | Return the ceiling of *x*, the smallest integer greater than or equal to *x*. |
| 35 | If *x* is not a float, delegates to ``x.__ceil__()``, which should return an |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 36 | :class:`~numbers.Integral` value. |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | .. function:: copysign(x, y) |
| 40 | |
Andrew Kuchling | 8cb1ec3 | 2014-02-16 11:11:25 -0500 | [diff] [blame] | 41 | Return a float with the magnitude (absolute value) of *x* but the sign of |
| 42 | *y*. On platforms that support signed zeros, ``copysign(1.0, -0.0)`` |
| 43 | returns *-1.0*. |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 44 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | .. function:: fabs(x) |
| 46 | |
| 47 | Return the absolute value of *x*. |
| 48 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 49 | .. function:: factorial(x) |
| 50 | |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 51 | Return *x* factorial. Raises :exc:`ValueError` if *x* is not integral or |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 52 | is negative. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
| 54 | .. function:: floor(x) |
| 55 | |
Georg Brandl | 2a03373 | 2008-04-05 17:37:09 +0000 | [diff] [blame] | 56 | Return the floor of *x*, the largest integer less than or equal to *x*. |
| 57 | If *x* is not a float, delegates to ``x.__floor__()``, which should return an |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 58 | :class:`~numbers.Integral` value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | .. function:: fmod(x, y) |
| 62 | |
| 63 | Return ``fmod(x, y)``, as defined by the platform C library. Note that the |
| 64 | Python expression ``x % y`` may not return the same result. The intent of the C |
| 65 | standard is that ``fmod(x, y)`` be exactly (mathematically; to infinite |
| 66 | precision) equal to ``x - n*y`` for some integer *n* such that the result has |
| 67 | the same sign as *x* and magnitude less than ``abs(y)``. Python's ``x % y`` |
| 68 | returns a result with the sign of *y* instead, and may not be exactly computable |
| 69 | for float arguments. For example, ``fmod(-1e-100, 1e100)`` is ``-1e-100``, but |
| 70 | the result of Python's ``-1e-100 % 1e100`` is ``1e100-1e-100``, which cannot be |
| 71 | represented exactly as a float, and rounds to the surprising ``1e100``. For |
| 72 | this reason, function :func:`fmod` is generally preferred when working with |
| 73 | floats, while Python's ``x % y`` is preferred when working with integers. |
| 74 | |
| 75 | |
| 76 | .. function:: frexp(x) |
| 77 | |
| 78 | Return the mantissa and exponent of *x* as the pair ``(m, e)``. *m* is a float |
| 79 | and *e* is an integer such that ``x == m * 2**e`` exactly. If *x* is zero, |
| 80 | returns ``(0.0, 0)``, otherwise ``0.5 <= abs(m) < 1``. This is used to "pick |
| 81 | apart" the internal representation of a float in a portable way. |
| 82 | |
| 83 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 84 | .. function:: fsum(iterable) |
| 85 | |
| 86 | Return an accurate floating point sum of values in the iterable. Avoids |
Raymond Hettinger | f3936f8 | 2009-02-19 05:48:05 +0000 | [diff] [blame] | 87 | loss of precision by tracking multiple intermediate partial sums:: |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 88 | |
Raymond Hettinger | f3936f8 | 2009-02-19 05:48:05 +0000 | [diff] [blame] | 89 | >>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]) |
Mark Dickinson | 5a55b61 | 2009-06-28 20:59:42 +0000 | [diff] [blame] | 90 | 0.9999999999999999 |
Raymond Hettinger | f3936f8 | 2009-02-19 05:48:05 +0000 | [diff] [blame] | 91 | >>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]) |
| 92 | 1.0 |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 93 | |
Raymond Hettinger | f3936f8 | 2009-02-19 05:48:05 +0000 | [diff] [blame] | 94 | The algorithm's accuracy depends on IEEE-754 arithmetic guarantees and the |
| 95 | typical case where the rounding mode is half-even. On some non-Windows |
| 96 | builds, the underlying C library uses extended precision addition and may |
| 97 | occasionally double-round an intermediate sum causing it to be off in its |
| 98 | least significant bit. |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 99 | |
Raymond Hettinger | 477be82 | 2009-02-19 06:44:30 +0000 | [diff] [blame] | 100 | For further discussion and two alternative approaches, see the `ASPN cookbook |
| 101 | recipes for accurate floating point summation |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 102 | <https://code.activestate.com/recipes/393090/>`_\. |
Raymond Hettinger | 477be82 | 2009-02-19 06:44:30 +0000 | [diff] [blame] | 103 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 104 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 105 | .. function:: gcd(a, b) |
| 106 | |
| 107 | Return the greatest common divisor of the integers *a* and *b*. If either |
| 108 | *a* or *b* is nonzero, then the value of ``gcd(a, b)`` is the largest |
| 109 | positive integer that divides both *a* and *b*. ``gcd(0, 0)`` returns |
| 110 | ``0``. |
| 111 | |
Benjamin Peterson | e960d18 | 2015-05-12 17:24:17 -0400 | [diff] [blame] | 112 | .. versionadded:: 3.5 |
| 113 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 114 | |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 115 | .. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) |
| 116 | |
| 117 | Return ``True`` if the values *a* and *b* are close to each other and |
| 118 | ``False`` otherwise. |
| 119 | |
| 120 | Whether or not two values are considered close is determined according to |
| 121 | given absolute and relative tolerances. |
| 122 | |
| 123 | *rel_tol* is the relative tolerance -- it is the maximum allowed difference |
| 124 | between *a* and *b*, relative to the larger absolute value of *a* or *b*. |
| 125 | For example, to set a tolerance of 5%, pass ``rel_tol=0.05``. The default |
| 126 | tolerance is ``1e-09``, which assures that the two values are the same |
| 127 | within about 9 decimal digits. *rel_tol* must be greater than zero. |
| 128 | |
| 129 | *abs_tol* is the minimum absolute tolerance -- useful for comparisons near |
| 130 | zero. *abs_tol* must be at least zero. |
| 131 | |
| 132 | If no errors occur, the result will be: |
| 133 | ``abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)``. |
| 134 | |
| 135 | The IEEE 754 special values of ``NaN``, ``inf``, and ``-inf`` will be |
| 136 | handled according to IEEE rules. Specifically, ``NaN`` is not considered |
| 137 | close to any other value, including ``NaN``. ``inf`` and ``-inf`` are only |
| 138 | considered close to themselves. |
| 139 | |
| 140 | .. versionadded:: 3.5 |
| 141 | |
| 142 | .. seealso:: |
| 143 | |
| 144 | :pep:`485` -- A function for testing approximate equality |
| 145 | |
| 146 | |
Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 147 | .. function:: isfinite(x) |
| 148 | |
| 149 | Return ``True`` if *x* is neither an infinity nor a NaN, and |
| 150 | ``False`` otherwise. (Note that ``0.0`` *is* considered finite.) |
| 151 | |
Mark Dickinson | c762242 | 2010-07-11 19:47:37 +0000 | [diff] [blame] | 152 | .. versionadded:: 3.2 |
| 153 | |
Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 154 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 155 | .. function:: isinf(x) |
| 156 | |
Mark Dickinson | c762242 | 2010-07-11 19:47:37 +0000 | [diff] [blame] | 157 | Return ``True`` if *x* is a positive or negative infinity, and |
| 158 | ``False`` otherwise. |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 159 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 160 | |
| 161 | .. function:: isnan(x) |
| 162 | |
Mark Dickinson | c762242 | 2010-07-11 19:47:37 +0000 | [diff] [blame] | 163 | Return ``True`` if *x* is a NaN (not a number), and ``False`` otherwise. |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 164 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 165 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 166 | .. function:: ldexp(x, i) |
| 167 | |
| 168 | Return ``x * (2**i)``. This is essentially the inverse of function |
| 169 | :func:`frexp`. |
| 170 | |
| 171 | |
| 172 | .. function:: modf(x) |
| 173 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 174 | Return the fractional and integer parts of *x*. Both results carry the sign |
| 175 | of *x* and are floats. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 177 | |
| 178 | .. function:: trunc(x) |
| 179 | |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 180 | Return the :class:`~numbers.Real` value *x* truncated to an |
| 181 | :class:`~numbers.Integral` (usually an integer). Delegates to |
| 182 | ``x.__trunc__()``. |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 183 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 184 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 185 | Note that :func:`frexp` and :func:`modf` have a different call/return pattern |
| 186 | than their C equivalents: they take a single argument and return a pair of |
| 187 | values, rather than returning their second return value through an 'output |
| 188 | parameter' (there is no such thing in Python). |
| 189 | |
| 190 | For the :func:`ceil`, :func:`floor`, and :func:`modf` functions, note that *all* |
| 191 | floating-point numbers of sufficiently large magnitude are exact integers. |
| 192 | Python floats typically carry no more than 53 bits of precision (the same as the |
| 193 | platform C double type), in which case any float *x* with ``abs(x) >= 2**52`` |
| 194 | necessarily has no fractional bits. |
| 195 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 196 | |
| 197 | Power and logarithmic functions |
| 198 | ------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 200 | .. function:: exp(x) |
| 201 | |
| 202 | Return ``e**x``. |
| 203 | |
| 204 | |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 205 | .. function:: expm1(x) |
| 206 | |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 207 | Return ``e**x - 1``. For small floats *x*, the subtraction in ``exp(x) - 1`` |
| 208 | can result in a `significant loss of precision |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 209 | <https://en.wikipedia.org/wiki/Loss_of_significance>`_\; the :func:`expm1` |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 210 | function provides a way to compute this quantity to full precision:: |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 211 | |
| 212 | >>> from math import exp, expm1 |
| 213 | >>> exp(1e-5) - 1 # gives result accurate to 11 places |
| 214 | 1.0000050000069649e-05 |
| 215 | >>> expm1(1e-5) # result accurate to full precision |
| 216 | 1.0000050000166668e-05 |
| 217 | |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 218 | .. versionadded:: 3.2 |
| 219 | |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 220 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 221 | .. function:: log(x[, base]) |
| 222 | |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 223 | With one argument, return the natural logarithm of *x* (to base *e*). |
| 224 | |
| 225 | With two arguments, return the logarithm of *x* to the given *base*, |
| 226 | calculated as ``log(x)/log(base)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 227 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 228 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 229 | .. function:: log1p(x) |
| 230 | |
| 231 | Return the natural logarithm of *1+x* (base *e*). The |
| 232 | result is calculated in a way which is accurate for *x* near zero. |
| 233 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 234 | |
Victor Stinner | fa0e3d5 | 2011-05-09 01:01:09 +0200 | [diff] [blame] | 235 | .. function:: log2(x) |
| 236 | |
Benjamin Peterson | eaee138 | 2011-05-08 19:48:08 -0500 | [diff] [blame] | 237 | Return the base-2 logarithm of *x*. This is usually more accurate than |
| 238 | ``log(x, 2)``. |
Victor Stinner | fa0e3d5 | 2011-05-09 01:01:09 +0200 | [diff] [blame] | 239 | |
| 240 | .. versionadded:: 3.3 |
| 241 | |
Victor Stinner | 9415afc | 2011-09-21 03:35:18 +0200 | [diff] [blame] | 242 | .. seealso:: |
| 243 | |
| 244 | :meth:`int.bit_length` returns the number of bits necessary to represent |
| 245 | an integer in binary, excluding the sign and leading zeros. |
| 246 | |
Victor Stinner | fa0e3d5 | 2011-05-09 01:01:09 +0200 | [diff] [blame] | 247 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 248 | .. function:: log10(x) |
| 249 | |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 250 | Return the base-10 logarithm of *x*. This is usually more accurate |
| 251 | than ``log(x, 10)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 252 | |
| 253 | |
| 254 | .. function:: pow(x, y) |
| 255 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 256 | Return ``x`` raised to the power ``y``. Exceptional cases follow |
| 257 | Annex 'F' of the C99 standard as far as possible. In particular, |
| 258 | ``pow(1.0, x)`` and ``pow(x, 0.0)`` always return ``1.0``, even |
| 259 | when ``x`` is a zero or a NaN. If both ``x`` and ``y`` are finite, |
| 260 | ``x`` is negative, and ``y`` is not an integer then ``pow(x, y)`` |
| 261 | is undefined, and raises :exc:`ValueError`. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 262 | |
Ezio Melotti | 739d549 | 2013-02-23 04:53:44 +0200 | [diff] [blame] | 263 | Unlike the built-in ``**`` operator, :func:`math.pow` converts both |
| 264 | its arguments to type :class:`float`. Use ``**`` or the built-in |
| 265 | :func:`pow` function for computing exact integer powers. |
| 266 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
| 268 | .. function:: sqrt(x) |
| 269 | |
| 270 | Return the square root of *x*. |
| 271 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 272 | Trigonometric functions |
| 273 | ----------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 274 | |
| 275 | |
| 276 | .. function:: acos(x) |
| 277 | |
| 278 | Return the arc cosine of *x*, in radians. |
| 279 | |
| 280 | |
| 281 | .. function:: asin(x) |
| 282 | |
| 283 | Return the arc sine of *x*, in radians. |
| 284 | |
| 285 | |
| 286 | .. function:: atan(x) |
| 287 | |
| 288 | Return the arc tangent of *x*, in radians. |
| 289 | |
| 290 | |
| 291 | .. function:: atan2(y, x) |
| 292 | |
| 293 | Return ``atan(y / x)``, in radians. The result is between ``-pi`` and ``pi``. |
| 294 | The vector in the plane from the origin to point ``(x, y)`` makes this angle |
| 295 | with the positive X axis. The point of :func:`atan2` is that the signs of both |
| 296 | inputs are known to it, so it can compute the correct quadrant for the angle. |
Mark Dickinson | 603b753 | 2010-04-06 19:55:03 +0000 | [diff] [blame] | 297 | For example, ``atan(1)`` and ``atan2(1, 1)`` are both ``pi/4``, but ``atan2(-1, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 | -1)`` is ``-3*pi/4``. |
| 299 | |
| 300 | |
| 301 | .. function:: cos(x) |
| 302 | |
| 303 | Return the cosine of *x* radians. |
| 304 | |
| 305 | |
| 306 | .. function:: hypot(x, y) |
| 307 | |
| 308 | Return the Euclidean norm, ``sqrt(x*x + y*y)``. This is the length of the vector |
| 309 | from the origin to point ``(x, y)``. |
| 310 | |
| 311 | |
| 312 | .. function:: sin(x) |
| 313 | |
| 314 | Return the sine of *x* radians. |
| 315 | |
| 316 | |
| 317 | .. function:: tan(x) |
| 318 | |
| 319 | Return the tangent of *x* radians. |
| 320 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 321 | Angular conversion |
| 322 | ------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 323 | |
| 324 | |
| 325 | .. function:: degrees(x) |
| 326 | |
Benjamin Peterson | 19a3f17 | 2015-05-12 19:15:53 -0400 | [diff] [blame] | 327 | Convert angle *x* from radians to degrees. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 328 | |
| 329 | |
| 330 | .. function:: radians(x) |
| 331 | |
Benjamin Peterson | 19a3f17 | 2015-05-12 19:15:53 -0400 | [diff] [blame] | 332 | Convert angle *x* from degrees to radians. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 333 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 334 | Hyperbolic functions |
| 335 | -------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 336 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 337 | `Hyperbolic functions <https://en.wikipedia.org/wiki/Hyperbolic_function>`_ |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 338 | are analogs of trigonometric functions that are based on hyperbolas |
| 339 | instead of circles. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 340 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 341 | .. function:: acosh(x) |
| 342 | |
| 343 | Return the inverse hyperbolic cosine of *x*. |
| 344 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 345 | |
| 346 | .. function:: asinh(x) |
| 347 | |
| 348 | Return the inverse hyperbolic sine of *x*. |
| 349 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 350 | |
| 351 | .. function:: atanh(x) |
| 352 | |
| 353 | Return the inverse hyperbolic tangent of *x*. |
| 354 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 355 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 356 | .. function:: cosh(x) |
| 357 | |
| 358 | Return the hyperbolic cosine of *x*. |
| 359 | |
| 360 | |
| 361 | .. function:: sinh(x) |
| 362 | |
| 363 | Return the hyperbolic sine of *x*. |
| 364 | |
| 365 | |
| 366 | .. function:: tanh(x) |
| 367 | |
| 368 | Return the hyperbolic tangent of *x*. |
| 369 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 370 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 371 | Special functions |
| 372 | ----------------- |
| 373 | |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 374 | .. function:: erf(x) |
| 375 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 376 | Return the `error function <https://en.wikipedia.org/wiki/Error_function>`_ at |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 377 | *x*. |
| 378 | |
| 379 | The :func:`erf` function can be used to compute traditional statistical |
| 380 | functions such as the `cumulative standard normal distribution |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 381 | <https://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution_function>`_:: |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 382 | |
| 383 | def phi(x): |
| 384 | 'Cumulative distribution function for the standard normal distribution' |
| 385 | return (1.0 + erf(x / sqrt(2.0))) / 2.0 |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 386 | |
| 387 | .. versionadded:: 3.2 |
| 388 | |
| 389 | |
| 390 | .. function:: erfc(x) |
| 391 | |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 392 | Return the complementary error function at *x*. The `complementary error |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 393 | function <https://en.wikipedia.org/wiki/Error_function>`_ is defined as |
Raymond Hettinger | 12e6c25 | 2011-03-31 13:59:24 -0700 | [diff] [blame] | 394 | ``1.0 - erf(x)``. It is used for large values of *x* where a subtraction |
| 395 | from one would cause a `loss of significance |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 396 | <https://en.wikipedia.org/wiki/Loss_of_significance>`_\. |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 397 | |
| 398 | .. versionadded:: 3.2 |
| 399 | |
| 400 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 401 | .. function:: gamma(x) |
| 402 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 403 | Return the `Gamma function <https://en.wikipedia.org/wiki/Gamma_function>`_ at |
Raymond Hettinger | 12e6c25 | 2011-03-31 13:59:24 -0700 | [diff] [blame] | 404 | *x*. |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 405 | |
Mark Dickinson | 56e0966 | 2009-10-01 16:13:29 +0000 | [diff] [blame] | 406 | .. versionadded:: 3.2 |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 407 | |
| 408 | |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 409 | .. function:: lgamma(x) |
| 410 | |
| 411 | Return the natural logarithm of the absolute value of the Gamma |
| 412 | function at *x*. |
| 413 | |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 414 | .. versionadded:: 3.2 |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 415 | |
| 416 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 417 | Constants |
Mark Dickinson | 60fe6b0 | 2009-06-02 12:53:15 +0000 | [diff] [blame] | 418 | --------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | |
| 420 | .. data:: pi |
| 421 | |
Mark Dickinson | 603b753 | 2010-04-06 19:55:03 +0000 | [diff] [blame] | 422 | The mathematical constant π = 3.141592..., to available precision. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 423 | |
| 424 | |
| 425 | .. data:: e |
| 426 | |
Mark Dickinson | 603b753 | 2010-04-06 19:55:03 +0000 | [diff] [blame] | 427 | The mathematical constant e = 2.718281..., to available precision. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 428 | |
Guido van Rossum | 0a891d7 | 2016-08-15 09:12:52 -0700 | [diff] [blame] | 429 | .. data:: tau |
| 430 | |
| 431 | The mathematical constant τ = 6.283185..., to available precision. |
| 432 | Tau is a circle constant equal to 2π, the ratio of a circle's circumference to |
| 433 | its radius. To learn more about Tau, check out Vi Hart's video `Pi is (still) |
| 434 | Wrong <https://www.youtube.com/watch?v=jG7vhMMXagQ>`_, and start celebrating |
| 435 | `Tau day <http://tauday.com/>`_ by eating twice as much pie! |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 436 | |
Georg Brandl | 4770d6e | 2016-08-16 07:08:46 +0200 | [diff] [blame] | 437 | .. versionadded:: 3.6 |
| 438 | |
Mark Dickinson | a5d0c7c | 2015-01-11 11:55:29 +0000 | [diff] [blame] | 439 | .. data:: inf |
| 440 | |
| 441 | A floating-point positive infinity. (For negative infinity, use |
| 442 | ``-math.inf``.) Equivalent to the output of ``float('inf')``. |
| 443 | |
| 444 | .. versionadded:: 3.5 |
| 445 | |
| 446 | |
| 447 | .. data:: nan |
| 448 | |
| 449 | A floating-point "not a number" (NaN) value. Equivalent to the output of |
| 450 | ``float('nan')``. |
| 451 | |
| 452 | .. versionadded:: 3.5 |
| 453 | |
| 454 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 455 | .. impl-detail:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 456 | |
| 457 | The :mod:`math` module consists mostly of thin wrappers around the platform C |
Mark Dickinson | 603b753 | 2010-04-06 19:55:03 +0000 | [diff] [blame] | 458 | math library functions. Behavior in exceptional cases follows Annex F of |
| 459 | the C99 standard where appropriate. The current implementation will raise |
| 460 | :exc:`ValueError` for invalid operations like ``sqrt(-1.0)`` or ``log(0.0)`` |
| 461 | (where C99 Annex F recommends signaling invalid operation or divide-by-zero), |
| 462 | and :exc:`OverflowError` for results that overflow (for example, |
Benjamin Peterson | 08bf91c | 2010-04-11 16:12:57 +0000 | [diff] [blame] | 463 | ``exp(1000.0)``). A NaN will not be returned from any of the functions |
| 464 | above unless one or more of the input arguments was a NaN; in that case, |
| 465 | most functions will return a NaN, but (again following C99 Annex F) there |
Mark Dickinson | 603b753 | 2010-04-06 19:55:03 +0000 | [diff] [blame] | 466 | are some exceptions to this rule, for example ``pow(float('nan'), 0.0)`` or |
| 467 | ``hypot(float('nan'), float('inf'))``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 468 | |
Mark Dickinson | 42dfeec | 2010-04-06 22:13:37 +0000 | [diff] [blame] | 469 | Note that Python makes no effort to distinguish signaling NaNs from |
| 470 | quiet NaNs, and behavior for signaling NaNs remains unspecified. |
| 471 | Typical behavior is to treat all NaNs as though they were quiet. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 472 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 473 | |
| 474 | .. seealso:: |
| 475 | |
| 476 | Module :mod:`cmath` |
| 477 | Complex number versions of many of these functions. |