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 | |
Ned Batchelder | 6faad35 | 2019-05-17 05:59:14 -0400 | [diff] [blame] | 13 | This module provides access to the mathematical functions defined by the C |
| 14 | standard. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Raymond Hettinger | b7fade4 | 2019-06-01 15:01:46 -0700 | [diff] [blame] | 39 | .. function:: comb(n, k) |
| 40 | |
| 41 | Return the number of ways to choose *k* items from *n* items without repetition |
| 42 | and without order. |
| 43 | |
Raymond Hettinger | 963eb0f | 2019-06-04 01:23:06 -0700 | [diff] [blame] | 44 | Evaluates to ``n! / (k! * (n - k)!)`` when ``k <= n`` and evaluates |
| 45 | to zero when ``k > n``. |
Raymond Hettinger | b7fade4 | 2019-06-01 15:01:46 -0700 | [diff] [blame] | 46 | |
Raymond Hettinger | 963eb0f | 2019-06-04 01:23:06 -0700 | [diff] [blame] | 47 | Also called the binomial coefficient because it is equivalent |
| 48 | to the coefficient of k-th term in polynomial expansion of the |
| 49 | expression ``(1 + x) ** n``. |
| 50 | |
Raymond Hettinger | 8f4bbb5 | 2019-06-04 03:40:23 -0700 | [diff] [blame] | 51 | Raises :exc:`TypeError` if either of the arguments are not integers. |
Raymond Hettinger | 963eb0f | 2019-06-04 01:23:06 -0700 | [diff] [blame] | 52 | Raises :exc:`ValueError` if either of the arguments are negative. |
Raymond Hettinger | b7fade4 | 2019-06-01 15:01:46 -0700 | [diff] [blame] | 53 | |
| 54 | .. versionadded:: 3.8 |
| 55 | |
| 56 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 57 | .. function:: copysign(x, y) |
| 58 | |
Andrew Kuchling | 8cb1ec3 | 2014-02-16 11:11:25 -0500 | [diff] [blame] | 59 | Return a float with the magnitude (absolute value) of *x* but the sign of |
| 60 | *y*. On platforms that support signed zeros, ``copysign(1.0, -0.0)`` |
| 61 | returns *-1.0*. |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 62 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 63 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | .. function:: fabs(x) |
| 65 | |
| 66 | Return the absolute value of *x*. |
| 67 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 68 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 69 | .. function:: factorial(x) |
| 70 | |
Akshay Sharma | 4612671 | 2019-05-31 22:11:17 +0530 | [diff] [blame] | 71 | Return *x* factorial as an integer. Raises :exc:`ValueError` if *x* is not integral or |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 72 | is negative. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
Serhiy Storchaka | 231aad3 | 2019-06-17 16:57:27 +0300 | [diff] [blame] | 74 | .. deprecated:: 3.9 |
| 75 | Accepting floats with integral values (like ``5.0``) is deprecated. |
| 76 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 77 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | .. function:: floor(x) |
| 79 | |
Georg Brandl | 2a03373 | 2008-04-05 17:37:09 +0000 | [diff] [blame] | 80 | Return the floor of *x*, the largest integer less than or equal to *x*. |
| 81 | 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] | 82 | :class:`~numbers.Integral` value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | .. function:: fmod(x, y) |
| 86 | |
| 87 | Return ``fmod(x, y)``, as defined by the platform C library. Note that the |
| 88 | Python expression ``x % y`` may not return the same result. The intent of the C |
| 89 | standard is that ``fmod(x, y)`` be exactly (mathematically; to infinite |
| 90 | precision) equal to ``x - n*y`` for some integer *n* such that the result has |
| 91 | the same sign as *x* and magnitude less than ``abs(y)``. Python's ``x % y`` |
| 92 | returns a result with the sign of *y* instead, and may not be exactly computable |
| 93 | for float arguments. For example, ``fmod(-1e-100, 1e100)`` is ``-1e-100``, but |
| 94 | the result of Python's ``-1e-100 % 1e100`` is ``1e100-1e-100``, which cannot be |
| 95 | represented exactly as a float, and rounds to the surprising ``1e100``. For |
| 96 | this reason, function :func:`fmod` is generally preferred when working with |
| 97 | floats, while Python's ``x % y`` is preferred when working with integers. |
| 98 | |
| 99 | |
| 100 | .. function:: frexp(x) |
| 101 | |
| 102 | Return the mantissa and exponent of *x* as the pair ``(m, e)``. *m* is a float |
| 103 | and *e* is an integer such that ``x == m * 2**e`` exactly. If *x* is zero, |
| 104 | returns ``(0.0, 0)``, otherwise ``0.5 <= abs(m) < 1``. This is used to "pick |
| 105 | apart" the internal representation of a float in a portable way. |
| 106 | |
| 107 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 108 | .. function:: fsum(iterable) |
| 109 | |
| 110 | Return an accurate floating point sum of values in the iterable. Avoids |
Raymond Hettinger | f3936f8 | 2009-02-19 05:48:05 +0000 | [diff] [blame] | 111 | loss of precision by tracking multiple intermediate partial sums:: |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 112 | |
Raymond Hettinger | f3936f8 | 2009-02-19 05:48:05 +0000 | [diff] [blame] | 113 | >>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]) |
Mark Dickinson | 5a55b61 | 2009-06-28 20:59:42 +0000 | [diff] [blame] | 114 | 0.9999999999999999 |
Raymond Hettinger | f3936f8 | 2009-02-19 05:48:05 +0000 | [diff] [blame] | 115 | >>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]) |
| 116 | 1.0 |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 117 | |
Raymond Hettinger | f3936f8 | 2009-02-19 05:48:05 +0000 | [diff] [blame] | 118 | The algorithm's accuracy depends on IEEE-754 arithmetic guarantees and the |
| 119 | typical case where the rounding mode is half-even. On some non-Windows |
| 120 | builds, the underlying C library uses extended precision addition and may |
| 121 | occasionally double-round an intermediate sum causing it to be off in its |
| 122 | least significant bit. |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 123 | |
Raymond Hettinger | 477be82 | 2009-02-19 06:44:30 +0000 | [diff] [blame] | 124 | For further discussion and two alternative approaches, see the `ASPN cookbook |
| 125 | recipes for accurate floating point summation |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 126 | <https://code.activestate.com/recipes/393090/>`_\. |
Raymond Hettinger | 477be82 | 2009-02-19 06:44:30 +0000 | [diff] [blame] | 127 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 128 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 129 | .. function:: gcd(a, b) |
| 130 | |
| 131 | Return the greatest common divisor of the integers *a* and *b*. If either |
| 132 | *a* or *b* is nonzero, then the value of ``gcd(a, b)`` is the largest |
| 133 | positive integer that divides both *a* and *b*. ``gcd(0, 0)`` returns |
| 134 | ``0``. |
| 135 | |
Benjamin Peterson | e960d18 | 2015-05-12 17:24:17 -0400 | [diff] [blame] | 136 | .. versionadded:: 3.5 |
| 137 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 138 | |
ananthan-123 | f2ee21d | 2020-02-19 23:51:37 +0530 | [diff] [blame^] | 139 | .. function:: lcm(a, b) |
| 140 | |
| 141 | Return the least common multiple of integers *a* and *b*. The value of |
| 142 | ``lcm(a, b)`` is the smallest nonnegative integer that is a multiple of |
| 143 | both *a* and *b*. If either *a* or *b* is zero then ``lcm(a, b)`` is zero. |
| 144 | |
| 145 | .. versionadded:: 3.9 |
| 146 | |
| 147 | |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 148 | .. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) |
| 149 | |
| 150 | Return ``True`` if the values *a* and *b* are close to each other and |
| 151 | ``False`` otherwise. |
| 152 | |
| 153 | Whether or not two values are considered close is determined according to |
| 154 | given absolute and relative tolerances. |
| 155 | |
| 156 | *rel_tol* is the relative tolerance -- it is the maximum allowed difference |
| 157 | between *a* and *b*, relative to the larger absolute value of *a* or *b*. |
| 158 | For example, to set a tolerance of 5%, pass ``rel_tol=0.05``. The default |
| 159 | tolerance is ``1e-09``, which assures that the two values are the same |
| 160 | within about 9 decimal digits. *rel_tol* must be greater than zero. |
| 161 | |
| 162 | *abs_tol* is the minimum absolute tolerance -- useful for comparisons near |
| 163 | zero. *abs_tol* must be at least zero. |
| 164 | |
| 165 | If no errors occur, the result will be: |
| 166 | ``abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)``. |
| 167 | |
| 168 | The IEEE 754 special values of ``NaN``, ``inf``, and ``-inf`` will be |
| 169 | handled according to IEEE rules. Specifically, ``NaN`` is not considered |
| 170 | close to any other value, including ``NaN``. ``inf`` and ``-inf`` are only |
| 171 | considered close to themselves. |
| 172 | |
| 173 | .. versionadded:: 3.5 |
| 174 | |
| 175 | .. seealso:: |
| 176 | |
| 177 | :pep:`485` -- A function for testing approximate equality |
| 178 | |
| 179 | |
Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 180 | .. function:: isfinite(x) |
| 181 | |
| 182 | Return ``True`` if *x* is neither an infinity nor a NaN, and |
| 183 | ``False`` otherwise. (Note that ``0.0`` *is* considered finite.) |
| 184 | |
Mark Dickinson | c762242 | 2010-07-11 19:47:37 +0000 | [diff] [blame] | 185 | .. versionadded:: 3.2 |
| 186 | |
Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 187 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 188 | .. function:: isinf(x) |
| 189 | |
Mark Dickinson | c762242 | 2010-07-11 19:47:37 +0000 | [diff] [blame] | 190 | Return ``True`` if *x* is a positive or negative infinity, and |
| 191 | ``False`` otherwise. |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 192 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 193 | |
| 194 | .. function:: isnan(x) |
| 195 | |
Mark Dickinson | c762242 | 2010-07-11 19:47:37 +0000 | [diff] [blame] | 196 | 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] | 197 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 198 | |
Mark Dickinson | 73934b9 | 2019-05-18 12:29:50 +0100 | [diff] [blame] | 199 | .. function:: isqrt(n) |
| 200 | |
| 201 | Return the integer square root of the nonnegative integer *n*. This is the |
| 202 | floor of the exact square root of *n*, or equivalently the greatest integer |
| 203 | *a* such that *a*\ ² |nbsp| ≤ |nbsp| *n*. |
| 204 | |
| 205 | For some applications, it may be more convenient to have the least integer |
| 206 | *a* such that *n* |nbsp| ≤ |nbsp| *a*\ ², or in other words the ceiling of |
| 207 | the exact square root of *n*. For positive *n*, this can be computed using |
| 208 | ``a = 1 + isqrt(n - 1)``. |
| 209 | |
| 210 | .. versionadded:: 3.8 |
| 211 | |
| 212 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 213 | .. function:: ldexp(x, i) |
| 214 | |
| 215 | Return ``x * (2**i)``. This is essentially the inverse of function |
| 216 | :func:`frexp`. |
| 217 | |
| 218 | |
| 219 | .. function:: modf(x) |
| 220 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 221 | Return the fractional and integer parts of *x*. Both results carry the sign |
| 222 | of *x* and are floats. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 224 | |
Victor Stinner | 100fafc | 2020-01-12 02:15:42 +0100 | [diff] [blame] | 225 | .. function:: nextafter(x, y) |
| 226 | |
| 227 | Return the next floating-point value after *x* towards *y*. |
| 228 | |
| 229 | If *x* is equal to *y*, return *y*. |
| 230 | |
Victor Stinner | 54cfbb2 | 2020-01-12 12:57:47 +0100 | [diff] [blame] | 231 | Examples: |
| 232 | |
| 233 | * ``math.nextafter(x, math.inf)`` goes up: towards positive infinity. |
| 234 | * ``math.nextafter(x, -math.inf)`` goes down: towards minus infinity. |
| 235 | * ``math.nextafter(x, 0.0)`` goes towards zero. |
| 236 | * ``math.nextafter(x, math.copysign(math.inf, x))`` goes away from zero. |
| 237 | |
Victor Stinner | 0b2ab21 | 2020-01-13 12:44:35 +0100 | [diff] [blame] | 238 | See also :func:`math.ulp`. |
| 239 | |
Victor Stinner | 100fafc | 2020-01-12 02:15:42 +0100 | [diff] [blame] | 240 | .. versionadded:: 3.9 |
| 241 | |
Raymond Hettinger | e119b3d | 2019-06-08 08:58:11 -0700 | [diff] [blame] | 242 | .. function:: perm(n, k=None) |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 243 | |
| 244 | Return the number of ways to choose *k* items from *n* items |
| 245 | without repetition and with order. |
| 246 | |
Raymond Hettinger | 963eb0f | 2019-06-04 01:23:06 -0700 | [diff] [blame] | 247 | Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates |
| 248 | to zero when ``k > n``. |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 249 | |
Raymond Hettinger | e119b3d | 2019-06-08 08:58:11 -0700 | [diff] [blame] | 250 | If *k* is not specified or is None, then *k* defaults to *n* |
| 251 | and the function returns ``n!``. |
| 252 | |
Raymond Hettinger | 8f4bbb5 | 2019-06-04 03:40:23 -0700 | [diff] [blame] | 253 | Raises :exc:`TypeError` if either of the arguments are not integers. |
Raymond Hettinger | 963eb0f | 2019-06-04 01:23:06 -0700 | [diff] [blame] | 254 | Raises :exc:`ValueError` if either of the arguments are negative. |
Serhiy Storchaka | 5ae299a | 2019-06-02 11:16:49 +0300 | [diff] [blame] | 255 | |
| 256 | .. versionadded:: 3.8 |
| 257 | |
| 258 | |
Pablo Galindo | bc09851 | 2019-02-07 07:04:02 +0000 | [diff] [blame] | 259 | .. function:: prod(iterable, *, start=1) |
| 260 | |
| 261 | Calculate the product of all the elements in the input *iterable*. |
| 262 | The default *start* value for the product is ``1``. |
| 263 | |
| 264 | When the iterable is empty, return the start value. This function is |
| 265 | intended specifically for use with numeric values and may reject |
| 266 | non-numeric types. |
| 267 | |
| 268 | .. versionadded:: 3.8 |
| 269 | |
| 270 | |
Mark Dickinson | a0ce375 | 2017-04-05 18:34:27 +0100 | [diff] [blame] | 271 | .. function:: remainder(x, y) |
| 272 | |
| 273 | Return the IEEE 754-style remainder of *x* with respect to *y*. For |
| 274 | finite *x* and finite nonzero *y*, this is the difference ``x - n*y``, |
| 275 | where ``n`` is the closest integer to the exact value of the quotient ``x / |
| 276 | y``. If ``x / y`` is exactly halfway between two consecutive integers, the |
| 277 | nearest *even* integer is used for ``n``. The remainder ``r = remainder(x, |
| 278 | y)`` thus always satisfies ``abs(r) <= 0.5 * abs(y)``. |
| 279 | |
| 280 | Special cases follow IEEE 754: in particular, ``remainder(x, math.inf)`` is |
| 281 | *x* for any finite *x*, and ``remainder(x, 0)`` and |
| 282 | ``remainder(math.inf, x)`` raise :exc:`ValueError` for any non-NaN *x*. |
| 283 | If the result of the remainder operation is zero, that zero will have |
| 284 | the same sign as *x*. |
| 285 | |
| 286 | On platforms using IEEE 754 binary floating-point, the result of this |
| 287 | operation is always exactly representable: no rounding error is introduced. |
| 288 | |
| 289 | .. versionadded:: 3.7 |
| 290 | |
| 291 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 292 | .. function:: trunc(x) |
| 293 | |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 294 | Return the :class:`~numbers.Real` value *x* truncated to an |
| 295 | :class:`~numbers.Integral` (usually an integer). Delegates to |
Eric Appelt | 308eab9 | 2018-03-10 02:44:12 -0600 | [diff] [blame] | 296 | :meth:`x.__trunc__() <object.__trunc__>`. |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 297 | |
Victor Stinner | 0b2ab21 | 2020-01-13 12:44:35 +0100 | [diff] [blame] | 298 | .. function:: ulp(x) |
| 299 | |
| 300 | Return the value of the least significant bit of the float *x*: |
| 301 | |
| 302 | * If *x* is a NaN (not a number), return *x*. |
| 303 | * If *x* is negative, return ``ulp(-x)``. |
| 304 | * If *x* is a positive infinity, return *x*. |
| 305 | * If *x* is equal to zero, return the smallest positive |
| 306 | *denormalized* representable float (smaller than the minimum positive |
| 307 | *normalized* float, :data:`sys.float_info.min <sys.float_info>`). |
| 308 | * If *x* is equal to the largest positive representable float, |
| 309 | return the value of the least significant bit of *x*, such that the first |
| 310 | float smaller than *x* is ``x - ulp(x)``. |
| 311 | * Otherwise (*x* is a positive finite number), return the value of the least |
| 312 | significant bit of *x*, such that the first float bigger than *x* |
| 313 | is ``x + ulp(x)``. |
| 314 | |
| 315 | ULP stands for "Unit in the Last Place". |
| 316 | |
| 317 | See also :func:`math.nextafter` and :data:`sys.float_info.epsilon |
| 318 | <sys.float_info>`. |
| 319 | |
| 320 | .. versionadded:: 3.9 |
| 321 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 322 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 323 | Note that :func:`frexp` and :func:`modf` have a different call/return pattern |
| 324 | than their C equivalents: they take a single argument and return a pair of |
| 325 | values, rather than returning their second return value through an 'output |
| 326 | parameter' (there is no such thing in Python). |
| 327 | |
| 328 | For the :func:`ceil`, :func:`floor`, and :func:`modf` functions, note that *all* |
| 329 | floating-point numbers of sufficiently large magnitude are exact integers. |
| 330 | Python floats typically carry no more than 53 bits of precision (the same as the |
| 331 | platform C double type), in which case any float *x* with ``abs(x) >= 2**52`` |
| 332 | necessarily has no fractional bits. |
| 333 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 334 | |
| 335 | Power and logarithmic functions |
| 336 | ------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 | .. function:: exp(x) |
| 339 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 340 | Return *e* raised to the power *x*, where *e* = 2.718281... is the base |
| 341 | of natural logarithms. This is usually more accurate than ``math.e ** x`` |
| 342 | or ``pow(math.e, x)``. |
| 343 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 344 | |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 345 | .. function:: expm1(x) |
| 346 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 347 | Return *e* raised to the power *x*, minus 1. Here *e* is the base of natural |
| 348 | logarithms. For small floats *x*, the subtraction in ``exp(x) - 1`` |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 349 | can result in a `significant loss of precision |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 350 | <https://en.wikipedia.org/wiki/Loss_of_significance>`_\; the :func:`expm1` |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 351 | function provides a way to compute this quantity to full precision:: |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 352 | |
| 353 | >>> from math import exp, expm1 |
| 354 | >>> exp(1e-5) - 1 # gives result accurate to 11 places |
| 355 | 1.0000050000069649e-05 |
| 356 | >>> expm1(1e-5) # result accurate to full precision |
| 357 | 1.0000050000166668e-05 |
| 358 | |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 359 | .. versionadded:: 3.2 |
| 360 | |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 361 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 362 | .. function:: log(x[, base]) |
| 363 | |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 364 | With one argument, return the natural logarithm of *x* (to base *e*). |
| 365 | |
| 366 | With two arguments, return the logarithm of *x* to the given *base*, |
| 367 | calculated as ``log(x)/log(base)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 368 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 369 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 370 | .. function:: log1p(x) |
| 371 | |
| 372 | Return the natural logarithm of *1+x* (base *e*). The |
| 373 | result is calculated in a way which is accurate for *x* near zero. |
| 374 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 375 | |
Victor Stinner | fa0e3d5 | 2011-05-09 01:01:09 +0200 | [diff] [blame] | 376 | .. function:: log2(x) |
| 377 | |
Benjamin Peterson | eaee138 | 2011-05-08 19:48:08 -0500 | [diff] [blame] | 378 | Return the base-2 logarithm of *x*. This is usually more accurate than |
| 379 | ``log(x, 2)``. |
Victor Stinner | fa0e3d5 | 2011-05-09 01:01:09 +0200 | [diff] [blame] | 380 | |
| 381 | .. versionadded:: 3.3 |
| 382 | |
Victor Stinner | 9415afc | 2011-09-21 03:35:18 +0200 | [diff] [blame] | 383 | .. seealso:: |
| 384 | |
| 385 | :meth:`int.bit_length` returns the number of bits necessary to represent |
| 386 | an integer in binary, excluding the sign and leading zeros. |
| 387 | |
Victor Stinner | fa0e3d5 | 2011-05-09 01:01:09 +0200 | [diff] [blame] | 388 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 389 | .. function:: log10(x) |
| 390 | |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 391 | Return the base-10 logarithm of *x*. This is usually more accurate |
| 392 | than ``log(x, 10)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 393 | |
| 394 | |
| 395 | .. function:: pow(x, y) |
| 396 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 397 | Return ``x`` raised to the power ``y``. Exceptional cases follow |
| 398 | Annex 'F' of the C99 standard as far as possible. In particular, |
| 399 | ``pow(1.0, x)`` and ``pow(x, 0.0)`` always return ``1.0``, even |
| 400 | when ``x`` is a zero or a NaN. If both ``x`` and ``y`` are finite, |
| 401 | ``x`` is negative, and ``y`` is not an integer then ``pow(x, y)`` |
| 402 | is undefined, and raises :exc:`ValueError`. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 403 | |
Ezio Melotti | 739d549 | 2013-02-23 04:53:44 +0200 | [diff] [blame] | 404 | Unlike the built-in ``**`` operator, :func:`math.pow` converts both |
| 405 | its arguments to type :class:`float`. Use ``**`` or the built-in |
| 406 | :func:`pow` function for computing exact integer powers. |
| 407 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 408 | |
| 409 | .. function:: sqrt(x) |
| 410 | |
| 411 | Return the square root of *x*. |
| 412 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 413 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 414 | Trigonometric functions |
| 415 | ----------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 416 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 417 | .. function:: acos(x) |
| 418 | |
Giovanni Cappellotto | dc3f99f | 2019-07-13 09:59:55 -0400 | [diff] [blame] | 419 | Return the arc cosine of *x*, in radians. The result is between ``0`` and |
| 420 | ``pi``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 421 | |
| 422 | |
| 423 | .. function:: asin(x) |
| 424 | |
Giovanni Cappellotto | dc3f99f | 2019-07-13 09:59:55 -0400 | [diff] [blame] | 425 | Return the arc sine of *x*, in radians. The result is between ``-pi/2`` and |
| 426 | ``pi/2``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | |
| 428 | |
| 429 | .. function:: atan(x) |
| 430 | |
Giovanni Cappellotto | dc3f99f | 2019-07-13 09:59:55 -0400 | [diff] [blame] | 431 | Return the arc tangent of *x*, in radians. The result is between ``-pi/2`` and |
| 432 | ``pi/2``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 433 | |
| 434 | |
| 435 | .. function:: atan2(y, x) |
| 436 | |
| 437 | Return ``atan(y / x)``, in radians. The result is between ``-pi`` and ``pi``. |
| 438 | The vector in the plane from the origin to point ``(x, y)`` makes this angle |
| 439 | with the positive X axis. The point of :func:`atan2` is that the signs of both |
| 440 | 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] | 441 | 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] | 442 | -1)`` is ``-3*pi/4``. |
| 443 | |
| 444 | |
| 445 | .. function:: cos(x) |
| 446 | |
| 447 | Return the cosine of *x* radians. |
| 448 | |
| 449 | |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 450 | .. function:: dist(p, q) |
| 451 | |
| 452 | Return the Euclidean distance between two points *p* and *q*, each |
Raymond Hettinger | 6b5f1b4 | 2019-07-27 14:04:29 -0700 | [diff] [blame] | 453 | given as a sequence (or iterable) of coordinates. The two points |
| 454 | must have the same dimension. |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 455 | |
| 456 | Roughly equivalent to:: |
| 457 | |
| 458 | sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q))) |
| 459 | |
| 460 | .. versionadded:: 3.8 |
| 461 | |
| 462 | |
Raymond Hettinger | c6dabe3 | 2018-07-28 07:48:04 -0700 | [diff] [blame] | 463 | .. function:: hypot(*coordinates) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 464 | |
Raymond Hettinger | c6dabe3 | 2018-07-28 07:48:04 -0700 | [diff] [blame] | 465 | Return the Euclidean norm, ``sqrt(sum(x**2 for x in coordinates))``. |
| 466 | This is the length of the vector from the origin to the point |
| 467 | given by the coordinates. |
| 468 | |
| 469 | For a two dimensional point ``(x, y)``, this is equivalent to computing |
| 470 | the hypotenuse of a right triangle using the Pythagorean theorem, |
| 471 | ``sqrt(x*x + y*y)``. |
| 472 | |
| 473 | .. versionchanged:: 3.8 |
| 474 | Added support for n-dimensional points. Formerly, only the two |
| 475 | dimensional case was supported. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 476 | |
| 477 | |
| 478 | .. function:: sin(x) |
| 479 | |
| 480 | Return the sine of *x* radians. |
| 481 | |
| 482 | |
| 483 | .. function:: tan(x) |
| 484 | |
| 485 | Return the tangent of *x* radians. |
| 486 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 487 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 488 | Angular conversion |
| 489 | ------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 490 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 491 | .. function:: degrees(x) |
| 492 | |
Benjamin Peterson | 19a3f17 | 2015-05-12 19:15:53 -0400 | [diff] [blame] | 493 | Convert angle *x* from radians to degrees. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 494 | |
| 495 | |
| 496 | .. function:: radians(x) |
| 497 | |
Benjamin Peterson | 19a3f17 | 2015-05-12 19:15:53 -0400 | [diff] [blame] | 498 | Convert angle *x* from degrees to radians. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 499 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 500 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 501 | Hyperbolic functions |
| 502 | -------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 503 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 504 | `Hyperbolic functions <https://en.wikipedia.org/wiki/Hyperbolic_function>`_ |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 505 | are analogs of trigonometric functions that are based on hyperbolas |
| 506 | instead of circles. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 507 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 508 | .. function:: acosh(x) |
| 509 | |
| 510 | Return the inverse hyperbolic cosine of *x*. |
| 511 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 512 | |
| 513 | .. function:: asinh(x) |
| 514 | |
| 515 | Return the inverse hyperbolic sine of *x*. |
| 516 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 517 | |
| 518 | .. function:: atanh(x) |
| 519 | |
| 520 | Return the inverse hyperbolic tangent of *x*. |
| 521 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 522 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 523 | .. function:: cosh(x) |
| 524 | |
| 525 | Return the hyperbolic cosine of *x*. |
| 526 | |
| 527 | |
| 528 | .. function:: sinh(x) |
| 529 | |
| 530 | Return the hyperbolic sine of *x*. |
| 531 | |
| 532 | |
| 533 | .. function:: tanh(x) |
| 534 | |
| 535 | Return the hyperbolic tangent of *x*. |
| 536 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 537 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 538 | Special functions |
| 539 | ----------------- |
| 540 | |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 541 | .. function:: erf(x) |
| 542 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 543 | Return the `error function <https://en.wikipedia.org/wiki/Error_function>`_ at |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 544 | *x*. |
| 545 | |
| 546 | The :func:`erf` function can be used to compute traditional statistical |
| 547 | functions such as the `cumulative standard normal distribution |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 548 | <https://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution_function>`_:: |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 549 | |
| 550 | def phi(x): |
| 551 | 'Cumulative distribution function for the standard normal distribution' |
| 552 | return (1.0 + erf(x / sqrt(2.0))) / 2.0 |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 553 | |
| 554 | .. versionadded:: 3.2 |
| 555 | |
| 556 | |
| 557 | .. function:: erfc(x) |
| 558 | |
Raymond Hettinger | 1081d48 | 2011-03-31 12:04:53 -0700 | [diff] [blame] | 559 | Return the complementary error function at *x*. The `complementary error |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 560 | function <https://en.wikipedia.org/wiki/Error_function>`_ is defined as |
Raymond Hettinger | 12e6c25 | 2011-03-31 13:59:24 -0700 | [diff] [blame] | 561 | ``1.0 - erf(x)``. It is used for large values of *x* where a subtraction |
| 562 | from one would cause a `loss of significance |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 563 | <https://en.wikipedia.org/wiki/Loss_of_significance>`_\. |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 564 | |
| 565 | .. versionadded:: 3.2 |
| 566 | |
| 567 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 568 | .. function:: gamma(x) |
| 569 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 570 | Return the `Gamma function <https://en.wikipedia.org/wiki/Gamma_function>`_ at |
Raymond Hettinger | 12e6c25 | 2011-03-31 13:59:24 -0700 | [diff] [blame] | 571 | *x*. |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 572 | |
Mark Dickinson | 56e0966 | 2009-10-01 16:13:29 +0000 | [diff] [blame] | 573 | .. versionadded:: 3.2 |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 574 | |
| 575 | |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 576 | .. function:: lgamma(x) |
| 577 | |
| 578 | Return the natural logarithm of the absolute value of the Gamma |
| 579 | function at *x*. |
| 580 | |
Mark Dickinson | 45f992a | 2009-12-19 11:20:49 +0000 | [diff] [blame] | 581 | .. versionadded:: 3.2 |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 582 | |
| 583 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 584 | Constants |
Mark Dickinson | 60fe6b0 | 2009-06-02 12:53:15 +0000 | [diff] [blame] | 585 | --------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 586 | |
| 587 | .. data:: pi |
| 588 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 589 | The mathematical constant *π* = 3.141592..., to available precision. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 590 | |
| 591 | |
| 592 | .. data:: e |
| 593 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 594 | The mathematical constant *e* = 2.718281..., to available precision. |
| 595 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | 0a891d7 | 2016-08-15 09:12:52 -0700 | [diff] [blame] | 597 | .. data:: tau |
| 598 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 599 | The mathematical constant *τ* = 6.283185..., to available precision. |
| 600 | Tau is a circle constant equal to 2\ *π*, the ratio of a circle's circumference to |
Guido van Rossum | 0a891d7 | 2016-08-15 09:12:52 -0700 | [diff] [blame] | 601 | its radius. To learn more about Tau, check out Vi Hart's video `Pi is (still) |
| 602 | Wrong <https://www.youtube.com/watch?v=jG7vhMMXagQ>`_, and start celebrating |
Sanyam Khurana | 338cd83 | 2018-01-20 05:55:37 +0530 | [diff] [blame] | 603 | `Tau day <https://tauday.com/>`_ by eating twice as much pie! |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 604 | |
Georg Brandl | 4770d6e | 2016-08-16 07:08:46 +0200 | [diff] [blame] | 605 | .. versionadded:: 3.6 |
| 606 | |
Serhiy Storchaka | dbaf746 | 2017-05-04 12:25:09 +0300 | [diff] [blame] | 607 | |
Mark Dickinson | a5d0c7c | 2015-01-11 11:55:29 +0000 | [diff] [blame] | 608 | .. data:: inf |
| 609 | |
| 610 | A floating-point positive infinity. (For negative infinity, use |
| 611 | ``-math.inf``.) Equivalent to the output of ``float('inf')``. |
| 612 | |
| 613 | .. versionadded:: 3.5 |
| 614 | |
| 615 | |
| 616 | .. data:: nan |
| 617 | |
| 618 | A floating-point "not a number" (NaN) value. Equivalent to the output of |
| 619 | ``float('nan')``. |
| 620 | |
| 621 | .. versionadded:: 3.5 |
| 622 | |
| 623 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 624 | .. impl-detail:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 625 | |
| 626 | 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] | 627 | math library functions. Behavior in exceptional cases follows Annex F of |
| 628 | the C99 standard where appropriate. The current implementation will raise |
| 629 | :exc:`ValueError` for invalid operations like ``sqrt(-1.0)`` or ``log(0.0)`` |
| 630 | (where C99 Annex F recommends signaling invalid operation or divide-by-zero), |
| 631 | and :exc:`OverflowError` for results that overflow (for example, |
Benjamin Peterson | 08bf91c | 2010-04-11 16:12:57 +0000 | [diff] [blame] | 632 | ``exp(1000.0)``). A NaN will not be returned from any of the functions |
| 633 | above unless one or more of the input arguments was a NaN; in that case, |
| 634 | 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] | 635 | are some exceptions to this rule, for example ``pow(float('nan'), 0.0)`` or |
| 636 | ``hypot(float('nan'), float('inf'))``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 637 | |
Mark Dickinson | 42dfeec | 2010-04-06 22:13:37 +0000 | [diff] [blame] | 638 | Note that Python makes no effort to distinguish signaling NaNs from |
| 639 | quiet NaNs, and behavior for signaling NaNs remains unspecified. |
| 640 | Typical behavior is to treat all NaNs as though they were quiet. |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 641 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 642 | |
| 643 | .. seealso:: |
| 644 | |
| 645 | Module :mod:`cmath` |
| 646 | Complex number versions of many of these functions. |
Mark Dickinson | 73934b9 | 2019-05-18 12:29:50 +0100 | [diff] [blame] | 647 | |
| 648 | .. |nbsp| unicode:: 0xA0 |
| 649 | :trim: |