blob: c4c180037f8788f74a92594c05bc8024b7ad34f9 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`math` --- Mathematical functions
2======================================
3
4.. module:: math
5 :synopsis: Mathematical functions (sin() etc.).
6
Łukasz Langa288234f2013-01-18 13:40:43 +01007.. testsetup::
8
9 from math import fsum
Georg Brandl116aa622007-08-15 14:28:22 +000010
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040011--------------
12
Ned Batchelder6faad352019-05-17 05:59:14 -040013This module provides access to the mathematical functions defined by the C
14standard.
Georg Brandl116aa622007-08-15 14:28:22 +000015
16These functions cannot be used with complex numbers; use the functions of the
17same name from the :mod:`cmath` module if you require support for complex
18numbers. The distinction between functions which support complex numbers and
19those which don't is made since most users do not want to learn quite as much
20mathematics as required to understand complex numbers. Receiving an exception
21instead of a complex result allows earlier detection of the unexpected complex
22number used as a parameter, so that the programmer can determine how and why it
23was generated in the first place.
24
25The following functions are provided by this module. Except when explicitly
26noted otherwise, all return values are floats.
27
Georg Brandl116aa622007-08-15 14:28:22 +000028
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +000029Number-theoretic and representation functions
30---------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +000031
32.. function:: ceil(x)
33
Georg Brandl2a033732008-04-05 17:37:09 +000034 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 Storchakabfdcd432013-10-13 23:09:14 +030036 :class:`~numbers.Integral` value.
Christian Heimes072c0f12008-01-03 23:01:04 +000037
38
Raymond Hettingerb7fade42019-06-01 15:01:46 -070039.. 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 Hettinger963eb0f2019-06-04 01:23:06 -070044 Evaluates to ``n! / (k! * (n - k)!)`` when ``k <= n`` and evaluates
45 to zero when ``k > n``.
Raymond Hettingerb7fade42019-06-01 15:01:46 -070046
Raymond Hettinger963eb0f2019-06-04 01:23:06 -070047 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 Hettinger8f4bbb52019-06-04 03:40:23 -070051 Raises :exc:`TypeError` if either of the arguments are not integers.
Raymond Hettinger963eb0f2019-06-04 01:23:06 -070052 Raises :exc:`ValueError` if either of the arguments are negative.
Raymond Hettingerb7fade42019-06-01 15:01:46 -070053
54 .. versionadded:: 3.8
55
56
Christian Heimes072c0f12008-01-03 23:01:04 +000057.. function:: copysign(x, y)
58
Andrew Kuchling8cb1ec32014-02-16 11:11:25 -050059 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 Heimes072c0f12008-01-03 23:01:04 +000062
Serhiy Storchakadbaf7462017-05-04 12:25:09 +030063
Georg Brandl116aa622007-08-15 14:28:22 +000064.. function:: fabs(x)
65
66 Return the absolute value of *x*.
67
Serhiy Storchakadbaf7462017-05-04 12:25:09 +030068
Georg Brandlc28e1fa2008-06-10 19:20:26 +000069.. function:: factorial(x)
70
Akshay Sharma46126712019-05-31 22:11:17 +053071 Return *x* factorial as an integer. Raises :exc:`ValueError` if *x* is not integral or
Georg Brandlc28e1fa2008-06-10 19:20:26 +000072 is negative.
Georg Brandl116aa622007-08-15 14:28:22 +000073
Serhiy Storchaka231aad32019-06-17 16:57:27 +030074 .. deprecated:: 3.9
75 Accepting floats with integral values (like ``5.0``) is deprecated.
76
Serhiy Storchakadbaf7462017-05-04 12:25:09 +030077
Georg Brandl116aa622007-08-15 14:28:22 +000078.. function:: floor(x)
79
Georg Brandl2a033732008-04-05 17:37:09 +000080 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 Storchakabfdcd432013-10-13 23:09:14 +030082 :class:`~numbers.Integral` value.
Georg Brandl116aa622007-08-15 14:28:22 +000083
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 Dickinsonaa7633a2008-08-01 08:16:13 +0000108.. function:: fsum(iterable)
109
110 Return an accurate floating point sum of values in the iterable. Avoids
Raymond Hettingerf3936f82009-02-19 05:48:05 +0000111 loss of precision by tracking multiple intermediate partial sums::
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000112
Raymond Hettingerf3936f82009-02-19 05:48:05 +0000113 >>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
Mark Dickinson5a55b612009-06-28 20:59:42 +0000114 0.9999999999999999
Raymond Hettingerf3936f82009-02-19 05:48:05 +0000115 >>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
116 1.0
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000117
Raymond Hettingerf3936f82009-02-19 05:48:05 +0000118 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 Dickinsonaa7633a2008-08-01 08:16:13 +0000123
Raymond Hettinger477be822009-02-19 06:44:30 +0000124 For further discussion and two alternative approaches, see the `ASPN cookbook
125 recipes for accurate floating point summation
Georg Brandl5d941342016-02-26 19:37:12 +0100126 <https://code.activestate.com/recipes/393090/>`_\.
Raymond Hettinger477be822009-02-19 06:44:30 +0000127
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000128
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +0300129.. 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 Petersone960d182015-05-12 17:24:17 -0400136 .. versionadded:: 3.5
137
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +0300138
Tal Einatd5519ed2015-05-31 22:05:00 +0300139.. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
140
141 Return ``True`` if the values *a* and *b* are close to each other and
142 ``False`` otherwise.
143
144 Whether or not two values are considered close is determined according to
145 given absolute and relative tolerances.
146
147 *rel_tol* is the relative tolerance -- it is the maximum allowed difference
148 between *a* and *b*, relative to the larger absolute value of *a* or *b*.
149 For example, to set a tolerance of 5%, pass ``rel_tol=0.05``. The default
150 tolerance is ``1e-09``, which assures that the two values are the same
151 within about 9 decimal digits. *rel_tol* must be greater than zero.
152
153 *abs_tol* is the minimum absolute tolerance -- useful for comparisons near
154 zero. *abs_tol* must be at least zero.
155
156 If no errors occur, the result will be:
157 ``abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)``.
158
159 The IEEE 754 special values of ``NaN``, ``inf``, and ``-inf`` will be
160 handled according to IEEE rules. Specifically, ``NaN`` is not considered
161 close to any other value, including ``NaN``. ``inf`` and ``-inf`` are only
162 considered close to themselves.
163
164 .. versionadded:: 3.5
165
166 .. seealso::
167
168 :pep:`485` -- A function for testing approximate equality
169
170
Mark Dickinson8e0c9962010-07-11 17:38:24 +0000171.. function:: isfinite(x)
172
173 Return ``True`` if *x* is neither an infinity nor a NaN, and
174 ``False`` otherwise. (Note that ``0.0`` *is* considered finite.)
175
Mark Dickinsonc7622422010-07-11 19:47:37 +0000176 .. versionadded:: 3.2
177
Mark Dickinson8e0c9962010-07-11 17:38:24 +0000178
Christian Heimes072c0f12008-01-03 23:01:04 +0000179.. function:: isinf(x)
180
Mark Dickinsonc7622422010-07-11 19:47:37 +0000181 Return ``True`` if *x* is a positive or negative infinity, and
182 ``False`` otherwise.
Christian Heimes072c0f12008-01-03 23:01:04 +0000183
Christian Heimes072c0f12008-01-03 23:01:04 +0000184
185.. function:: isnan(x)
186
Mark Dickinsonc7622422010-07-11 19:47:37 +0000187 Return ``True`` if *x* is a NaN (not a number), and ``False`` otherwise.
Christian Heimes072c0f12008-01-03 23:01:04 +0000188
Christian Heimes072c0f12008-01-03 23:01:04 +0000189
Mark Dickinson73934b92019-05-18 12:29:50 +0100190.. function:: isqrt(n)
191
192 Return the integer square root of the nonnegative integer *n*. This is the
193 floor of the exact square root of *n*, or equivalently the greatest integer
194 *a* such that *a*\ ² |nbsp| ≤ |nbsp| *n*.
195
196 For some applications, it may be more convenient to have the least integer
197 *a* such that *n* |nbsp| ≤ |nbsp| *a*\ ², or in other words the ceiling of
198 the exact square root of *n*. For positive *n*, this can be computed using
199 ``a = 1 + isqrt(n - 1)``.
200
201 .. versionadded:: 3.8
202
203
Georg Brandl116aa622007-08-15 14:28:22 +0000204.. function:: ldexp(x, i)
205
206 Return ``x * (2**i)``. This is essentially the inverse of function
207 :func:`frexp`.
208
209
210.. function:: modf(x)
211
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000212 Return the fractional and integer parts of *x*. Both results carry the sign
213 of *x* and are floats.
Georg Brandl116aa622007-08-15 14:28:22 +0000214
Christian Heimes400adb02008-02-01 08:12:03 +0000215
Victor Stinner100fafc2020-01-12 02:15:42 +0100216.. function:: nextafter(x, y)
217
218 Return the next floating-point value after *x* towards *y*.
219
220 If *x* is equal to *y*, return *y*.
221
Victor Stinner54cfbb22020-01-12 12:57:47 +0100222 Examples:
223
224 * ``math.nextafter(x, math.inf)`` goes up: towards positive infinity.
225 * ``math.nextafter(x, -math.inf)`` goes down: towards minus infinity.
226 * ``math.nextafter(x, 0.0)`` goes towards zero.
227 * ``math.nextafter(x, math.copysign(math.inf, x))`` goes away from zero.
228
Victor Stinner0b2ab212020-01-13 12:44:35 +0100229 See also :func:`math.ulp`.
230
Victor Stinner100fafc2020-01-12 02:15:42 +0100231 .. versionadded:: 3.9
232
Raymond Hettingere119b3d2019-06-08 08:58:11 -0700233.. function:: perm(n, k=None)
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300234
235 Return the number of ways to choose *k* items from *n* items
236 without repetition and with order.
237
Raymond Hettinger963eb0f2019-06-04 01:23:06 -0700238 Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates
239 to zero when ``k > n``.
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300240
Raymond Hettingere119b3d2019-06-08 08:58:11 -0700241 If *k* is not specified or is None, then *k* defaults to *n*
242 and the function returns ``n!``.
243
Raymond Hettinger8f4bbb52019-06-04 03:40:23 -0700244 Raises :exc:`TypeError` if either of the arguments are not integers.
Raymond Hettinger963eb0f2019-06-04 01:23:06 -0700245 Raises :exc:`ValueError` if either of the arguments are negative.
Serhiy Storchaka5ae299a2019-06-02 11:16:49 +0300246
247 .. versionadded:: 3.8
248
249
Pablo Galindobc098512019-02-07 07:04:02 +0000250.. function:: prod(iterable, *, start=1)
251
252 Calculate the product of all the elements in the input *iterable*.
253 The default *start* value for the product is ``1``.
254
255 When the iterable is empty, return the start value. This function is
256 intended specifically for use with numeric values and may reject
257 non-numeric types.
258
259 .. versionadded:: 3.8
260
261
Mark Dickinsona0ce3752017-04-05 18:34:27 +0100262.. function:: remainder(x, y)
263
264 Return the IEEE 754-style remainder of *x* with respect to *y*. For
265 finite *x* and finite nonzero *y*, this is the difference ``x - n*y``,
266 where ``n`` is the closest integer to the exact value of the quotient ``x /
267 y``. If ``x / y`` is exactly halfway between two consecutive integers, the
268 nearest *even* integer is used for ``n``. The remainder ``r = remainder(x,
269 y)`` thus always satisfies ``abs(r) <= 0.5 * abs(y)``.
270
271 Special cases follow IEEE 754: in particular, ``remainder(x, math.inf)`` is
272 *x* for any finite *x*, and ``remainder(x, 0)`` and
273 ``remainder(math.inf, x)`` raise :exc:`ValueError` for any non-NaN *x*.
274 If the result of the remainder operation is zero, that zero will have
275 the same sign as *x*.
276
277 On platforms using IEEE 754 binary floating-point, the result of this
278 operation is always exactly representable: no rounding error is introduced.
279
280 .. versionadded:: 3.7
281
282
Christian Heimes400adb02008-02-01 08:12:03 +0000283.. function:: trunc(x)
284
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300285 Return the :class:`~numbers.Real` value *x* truncated to an
286 :class:`~numbers.Integral` (usually an integer). Delegates to
Eric Appelt308eab92018-03-10 02:44:12 -0600287 :meth:`x.__trunc__() <object.__trunc__>`.
Christian Heimes400adb02008-02-01 08:12:03 +0000288
Victor Stinner0b2ab212020-01-13 12:44:35 +0100289.. function:: ulp(x)
290
291 Return the value of the least significant bit of the float *x*:
292
293 * If *x* is a NaN (not a number), return *x*.
294 * If *x* is negative, return ``ulp(-x)``.
295 * If *x* is a positive infinity, return *x*.
296 * If *x* is equal to zero, return the smallest positive
297 *denormalized* representable float (smaller than the minimum positive
298 *normalized* float, :data:`sys.float_info.min <sys.float_info>`).
299 * If *x* is equal to the largest positive representable float,
300 return the value of the least significant bit of *x*, such that the first
301 float smaller than *x* is ``x - ulp(x)``.
302 * Otherwise (*x* is a positive finite number), return the value of the least
303 significant bit of *x*, such that the first float bigger than *x*
304 is ``x + ulp(x)``.
305
306 ULP stands for "Unit in the Last Place".
307
308 See also :func:`math.nextafter` and :data:`sys.float_info.epsilon
309 <sys.float_info>`.
310
311 .. versionadded:: 3.9
312
Christian Heimes400adb02008-02-01 08:12:03 +0000313
Georg Brandl116aa622007-08-15 14:28:22 +0000314Note that :func:`frexp` and :func:`modf` have a different call/return pattern
315than their C equivalents: they take a single argument and return a pair of
316values, rather than returning their second return value through an 'output
317parameter' (there is no such thing in Python).
318
319For the :func:`ceil`, :func:`floor`, and :func:`modf` functions, note that *all*
320floating-point numbers of sufficiently large magnitude are exact integers.
321Python floats typically carry no more than 53 bits of precision (the same as the
322platform C double type), in which case any float *x* with ``abs(x) >= 2**52``
323necessarily has no fractional bits.
324
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000325
326Power and logarithmic functions
327-------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000328
Georg Brandl116aa622007-08-15 14:28:22 +0000329.. function:: exp(x)
330
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300331 Return *e* raised to the power *x*, where *e* = 2.718281... is the base
332 of natural logarithms. This is usually more accurate than ``math.e ** x``
333 or ``pow(math.e, x)``.
334
Georg Brandl116aa622007-08-15 14:28:22 +0000335
Mark Dickinson664b5112009-12-16 20:23:42 +0000336.. function:: expm1(x)
337
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300338 Return *e* raised to the power *x*, minus 1. Here *e* is the base of natural
339 logarithms. For small floats *x*, the subtraction in ``exp(x) - 1``
Raymond Hettinger1081d482011-03-31 12:04:53 -0700340 can result in a `significant loss of precision
Georg Brandl5d941342016-02-26 19:37:12 +0100341 <https://en.wikipedia.org/wiki/Loss_of_significance>`_\; the :func:`expm1`
Raymond Hettinger1081d482011-03-31 12:04:53 -0700342 function provides a way to compute this quantity to full precision::
Mark Dickinson664b5112009-12-16 20:23:42 +0000343
344 >>> from math import exp, expm1
345 >>> exp(1e-5) - 1 # gives result accurate to 11 places
346 1.0000050000069649e-05
347 >>> expm1(1e-5) # result accurate to full precision
348 1.0000050000166668e-05
349
Mark Dickinson45f992a2009-12-19 11:20:49 +0000350 .. versionadded:: 3.2
351
Mark Dickinson664b5112009-12-16 20:23:42 +0000352
Georg Brandl116aa622007-08-15 14:28:22 +0000353.. function:: log(x[, base])
354
Georg Brandla6053b42009-09-01 08:11:14 +0000355 With one argument, return the natural logarithm of *x* (to base *e*).
356
357 With two arguments, return the logarithm of *x* to the given *base*,
358 calculated as ``log(x)/log(base)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000359
Georg Brandl116aa622007-08-15 14:28:22 +0000360
Christian Heimes53876d92008-04-19 00:31:39 +0000361.. function:: log1p(x)
362
363 Return the natural logarithm of *1+x* (base *e*). The
364 result is calculated in a way which is accurate for *x* near zero.
365
Christian Heimes53876d92008-04-19 00:31:39 +0000366
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200367.. function:: log2(x)
368
Benjamin Petersoneaee1382011-05-08 19:48:08 -0500369 Return the base-2 logarithm of *x*. This is usually more accurate than
370 ``log(x, 2)``.
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200371
372 .. versionadded:: 3.3
373
Victor Stinner9415afc2011-09-21 03:35:18 +0200374 .. seealso::
375
376 :meth:`int.bit_length` returns the number of bits necessary to represent
377 an integer in binary, excluding the sign and leading zeros.
378
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200379
Georg Brandl116aa622007-08-15 14:28:22 +0000380.. function:: log10(x)
381
Georg Brandla6053b42009-09-01 08:11:14 +0000382 Return the base-10 logarithm of *x*. This is usually more accurate
383 than ``log(x, 10)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000384
385
386.. function:: pow(x, y)
387
Christian Heimesa342c012008-04-20 21:01:16 +0000388 Return ``x`` raised to the power ``y``. Exceptional cases follow
389 Annex 'F' of the C99 standard as far as possible. In particular,
390 ``pow(1.0, x)`` and ``pow(x, 0.0)`` always return ``1.0``, even
391 when ``x`` is a zero or a NaN. If both ``x`` and ``y`` are finite,
392 ``x`` is negative, and ``y`` is not an integer then ``pow(x, y)``
393 is undefined, and raises :exc:`ValueError`.
Christian Heimes53876d92008-04-19 00:31:39 +0000394
Ezio Melotti739d5492013-02-23 04:53:44 +0200395 Unlike the built-in ``**`` operator, :func:`math.pow` converts both
396 its arguments to type :class:`float`. Use ``**`` or the built-in
397 :func:`pow` function for computing exact integer powers.
398
Georg Brandl116aa622007-08-15 14:28:22 +0000399
400.. function:: sqrt(x)
401
402 Return the square root of *x*.
403
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300404
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000405Trigonometric functions
406-----------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000407
Georg Brandl116aa622007-08-15 14:28:22 +0000408.. function:: acos(x)
409
Giovanni Cappellottodc3f99f2019-07-13 09:59:55 -0400410 Return the arc cosine of *x*, in radians. The result is between ``0`` and
411 ``pi``.
Georg Brandl116aa622007-08-15 14:28:22 +0000412
413
414.. function:: asin(x)
415
Giovanni Cappellottodc3f99f2019-07-13 09:59:55 -0400416 Return the arc sine of *x*, in radians. The result is between ``-pi/2`` and
417 ``pi/2``.
Georg Brandl116aa622007-08-15 14:28:22 +0000418
419
420.. function:: atan(x)
421
Giovanni Cappellottodc3f99f2019-07-13 09:59:55 -0400422 Return the arc tangent of *x*, in radians. The result is between ``-pi/2`` and
423 ``pi/2``.
Georg Brandl116aa622007-08-15 14:28:22 +0000424
425
426.. function:: atan2(y, x)
427
428 Return ``atan(y / x)``, in radians. The result is between ``-pi`` and ``pi``.
429 The vector in the plane from the origin to point ``(x, y)`` makes this angle
430 with the positive X axis. The point of :func:`atan2` is that the signs of both
431 inputs are known to it, so it can compute the correct quadrant for the angle.
Mark Dickinson603b7532010-04-06 19:55:03 +0000432 For example, ``atan(1)`` and ``atan2(1, 1)`` are both ``pi/4``, but ``atan2(-1,
Georg Brandl116aa622007-08-15 14:28:22 +0000433 -1)`` is ``-3*pi/4``.
434
435
436.. function:: cos(x)
437
438 Return the cosine of *x* radians.
439
440
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700441.. function:: dist(p, q)
442
443 Return the Euclidean distance between two points *p* and *q*, each
Raymond Hettinger6b5f1b42019-07-27 14:04:29 -0700444 given as a sequence (or iterable) of coordinates. The two points
445 must have the same dimension.
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700446
447 Roughly equivalent to::
448
449 sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
450
451 .. versionadded:: 3.8
452
453
Raymond Hettingerc6dabe32018-07-28 07:48:04 -0700454.. function:: hypot(*coordinates)
Georg Brandl116aa622007-08-15 14:28:22 +0000455
Raymond Hettingerc6dabe32018-07-28 07:48:04 -0700456 Return the Euclidean norm, ``sqrt(sum(x**2 for x in coordinates))``.
457 This is the length of the vector from the origin to the point
458 given by the coordinates.
459
460 For a two dimensional point ``(x, y)``, this is equivalent to computing
461 the hypotenuse of a right triangle using the Pythagorean theorem,
462 ``sqrt(x*x + y*y)``.
463
464 .. versionchanged:: 3.8
465 Added support for n-dimensional points. Formerly, only the two
466 dimensional case was supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000467
468
469.. function:: sin(x)
470
471 Return the sine of *x* radians.
472
473
474.. function:: tan(x)
475
476 Return the tangent of *x* radians.
477
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300478
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000479Angular conversion
480------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000481
Georg Brandl116aa622007-08-15 14:28:22 +0000482.. function:: degrees(x)
483
Benjamin Peterson19a3f172015-05-12 19:15:53 -0400484 Convert angle *x* from radians to degrees.
Georg Brandl116aa622007-08-15 14:28:22 +0000485
486
487.. function:: radians(x)
488
Benjamin Peterson19a3f172015-05-12 19:15:53 -0400489 Convert angle *x* from degrees to radians.
Georg Brandl116aa622007-08-15 14:28:22 +0000490
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300491
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000492Hyperbolic functions
493--------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000494
Georg Brandl5d941342016-02-26 19:37:12 +0100495`Hyperbolic functions <https://en.wikipedia.org/wiki/Hyperbolic_function>`_
Raymond Hettinger1081d482011-03-31 12:04:53 -0700496are analogs of trigonometric functions that are based on hyperbolas
497instead of circles.
Georg Brandl116aa622007-08-15 14:28:22 +0000498
Christian Heimesa342c012008-04-20 21:01:16 +0000499.. function:: acosh(x)
500
501 Return the inverse hyperbolic cosine of *x*.
502
Christian Heimesa342c012008-04-20 21:01:16 +0000503
504.. function:: asinh(x)
505
506 Return the inverse hyperbolic sine of *x*.
507
Christian Heimesa342c012008-04-20 21:01:16 +0000508
509.. function:: atanh(x)
510
511 Return the inverse hyperbolic tangent of *x*.
512
Christian Heimesa342c012008-04-20 21:01:16 +0000513
Georg Brandl116aa622007-08-15 14:28:22 +0000514.. function:: cosh(x)
515
516 Return the hyperbolic cosine of *x*.
517
518
519.. function:: sinh(x)
520
521 Return the hyperbolic sine of *x*.
522
523
524.. function:: tanh(x)
525
526 Return the hyperbolic tangent of *x*.
527
Christian Heimes53876d92008-04-19 00:31:39 +0000528
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000529Special functions
530-----------------
531
Mark Dickinson45f992a2009-12-19 11:20:49 +0000532.. function:: erf(x)
533
Georg Brandl5d941342016-02-26 19:37:12 +0100534 Return the `error function <https://en.wikipedia.org/wiki/Error_function>`_ at
Raymond Hettinger1081d482011-03-31 12:04:53 -0700535 *x*.
536
537 The :func:`erf` function can be used to compute traditional statistical
538 functions such as the `cumulative standard normal distribution
Georg Brandl5d941342016-02-26 19:37:12 +0100539 <https://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution_function>`_::
Raymond Hettinger1081d482011-03-31 12:04:53 -0700540
541 def phi(x):
542 'Cumulative distribution function for the standard normal distribution'
543 return (1.0 + erf(x / sqrt(2.0))) / 2.0
Mark Dickinson45f992a2009-12-19 11:20:49 +0000544
545 .. versionadded:: 3.2
546
547
548.. function:: erfc(x)
549
Raymond Hettinger1081d482011-03-31 12:04:53 -0700550 Return the complementary error function at *x*. The `complementary error
Georg Brandl5d941342016-02-26 19:37:12 +0100551 function <https://en.wikipedia.org/wiki/Error_function>`_ is defined as
Raymond Hettinger12e6c252011-03-31 13:59:24 -0700552 ``1.0 - erf(x)``. It is used for large values of *x* where a subtraction
553 from one would cause a `loss of significance
Georg Brandl5d941342016-02-26 19:37:12 +0100554 <https://en.wikipedia.org/wiki/Loss_of_significance>`_\.
Mark Dickinson45f992a2009-12-19 11:20:49 +0000555
556 .. versionadded:: 3.2
557
558
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000559.. function:: gamma(x)
560
Georg Brandl5d941342016-02-26 19:37:12 +0100561 Return the `Gamma function <https://en.wikipedia.org/wiki/Gamma_function>`_ at
Raymond Hettinger12e6c252011-03-31 13:59:24 -0700562 *x*.
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000563
Mark Dickinson56e09662009-10-01 16:13:29 +0000564 .. versionadded:: 3.2
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000565
566
Mark Dickinson05d2e082009-12-11 20:17:17 +0000567.. function:: lgamma(x)
568
569 Return the natural logarithm of the absolute value of the Gamma
570 function at *x*.
571
Mark Dickinson45f992a2009-12-19 11:20:49 +0000572 .. versionadded:: 3.2
Mark Dickinson05d2e082009-12-11 20:17:17 +0000573
574
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000575Constants
Mark Dickinson60fe6b02009-06-02 12:53:15 +0000576---------
Georg Brandl116aa622007-08-15 14:28:22 +0000577
578.. data:: pi
579
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300580 The mathematical constant *π* = 3.141592..., to available precision.
Georg Brandl116aa622007-08-15 14:28:22 +0000581
582
583.. data:: e
584
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300585 The mathematical constant *e* = 2.718281..., to available precision.
586
Georg Brandl116aa622007-08-15 14:28:22 +0000587
Guido van Rossum0a891d72016-08-15 09:12:52 -0700588.. data:: tau
589
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300590 The mathematical constant *τ* = 6.283185..., to available precision.
591 Tau is a circle constant equal to 2\ *π*, the ratio of a circle's circumference to
Guido van Rossum0a891d72016-08-15 09:12:52 -0700592 its radius. To learn more about Tau, check out Vi Hart's video `Pi is (still)
593 Wrong <https://www.youtube.com/watch?v=jG7vhMMXagQ>`_, and start celebrating
Sanyam Khurana338cd832018-01-20 05:55:37 +0530594 `Tau day <https://tauday.com/>`_ by eating twice as much pie!
Christian Heimes53876d92008-04-19 00:31:39 +0000595
Georg Brandl4770d6e2016-08-16 07:08:46 +0200596 .. versionadded:: 3.6
597
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300598
Mark Dickinsona5d0c7c2015-01-11 11:55:29 +0000599.. data:: inf
600
601 A floating-point positive infinity. (For negative infinity, use
602 ``-math.inf``.) Equivalent to the output of ``float('inf')``.
603
604 .. versionadded:: 3.5
605
606
607.. data:: nan
608
609 A floating-point "not a number" (NaN) value. Equivalent to the output of
610 ``float('nan')``.
611
612 .. versionadded:: 3.5
613
614
Georg Brandl495f7b52009-10-27 15:28:25 +0000615.. impl-detail::
Georg Brandl116aa622007-08-15 14:28:22 +0000616
617 The :mod:`math` module consists mostly of thin wrappers around the platform C
Mark Dickinson603b7532010-04-06 19:55:03 +0000618 math library functions. Behavior in exceptional cases follows Annex F of
619 the C99 standard where appropriate. The current implementation will raise
620 :exc:`ValueError` for invalid operations like ``sqrt(-1.0)`` or ``log(0.0)``
621 (where C99 Annex F recommends signaling invalid operation or divide-by-zero),
622 and :exc:`OverflowError` for results that overflow (for example,
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000623 ``exp(1000.0)``). A NaN will not be returned from any of the functions
624 above unless one or more of the input arguments was a NaN; in that case,
625 most functions will return a NaN, but (again following C99 Annex F) there
Mark Dickinson603b7532010-04-06 19:55:03 +0000626 are some exceptions to this rule, for example ``pow(float('nan'), 0.0)`` or
627 ``hypot(float('nan'), float('inf'))``.
Georg Brandl116aa622007-08-15 14:28:22 +0000628
Mark Dickinson42dfeec2010-04-06 22:13:37 +0000629 Note that Python makes no effort to distinguish signaling NaNs from
630 quiet NaNs, and behavior for signaling NaNs remains unspecified.
631 Typical behavior is to treat all NaNs as though they were quiet.
Christian Heimes53876d92008-04-19 00:31:39 +0000632
Georg Brandl116aa622007-08-15 14:28:22 +0000633
634.. seealso::
635
636 Module :mod:`cmath`
637 Complex number versions of many of these functions.
Mark Dickinson73934b92019-05-18 12:29:50 +0100638
639.. |nbsp| unicode:: 0xA0
640 :trim: