blob: 206b06edd2a2065975290af3bc2d5b2297d34b80 [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
39.. function:: copysign(x, y)
40
Andrew Kuchling8cb1ec32014-02-16 11:11:25 -050041 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 Heimes072c0f12008-01-03 23:01:04 +000044
Serhiy Storchakadbaf7462017-05-04 12:25:09 +030045
Georg Brandl116aa622007-08-15 14:28:22 +000046.. function:: fabs(x)
47
48 Return the absolute value of *x*.
49
Serhiy Storchakadbaf7462017-05-04 12:25:09 +030050
Georg Brandlc28e1fa2008-06-10 19:20:26 +000051.. function:: factorial(x)
52
Akshay Sharma46126712019-05-31 22:11:17 +053053 Return *x* factorial as an integer. Raises :exc:`ValueError` if *x* is not integral or
Georg Brandlc28e1fa2008-06-10 19:20:26 +000054 is negative.
Georg Brandl116aa622007-08-15 14:28:22 +000055
Serhiy Storchakadbaf7462017-05-04 12:25:09 +030056
Georg Brandl116aa622007-08-15 14:28:22 +000057.. function:: floor(x)
58
Georg Brandl2a033732008-04-05 17:37:09 +000059 Return the floor of *x*, the largest integer less than or equal to *x*.
60 If *x* is not a float, delegates to ``x.__floor__()``, which should return an
Serhiy Storchakabfdcd432013-10-13 23:09:14 +030061 :class:`~numbers.Integral` value.
Georg Brandl116aa622007-08-15 14:28:22 +000062
63
64.. function:: fmod(x, y)
65
66 Return ``fmod(x, y)``, as defined by the platform C library. Note that the
67 Python expression ``x % y`` may not return the same result. The intent of the C
68 standard is that ``fmod(x, y)`` be exactly (mathematically; to infinite
69 precision) equal to ``x - n*y`` for some integer *n* such that the result has
70 the same sign as *x* and magnitude less than ``abs(y)``. Python's ``x % y``
71 returns a result with the sign of *y* instead, and may not be exactly computable
72 for float arguments. For example, ``fmod(-1e-100, 1e100)`` is ``-1e-100``, but
73 the result of Python's ``-1e-100 % 1e100`` is ``1e100-1e-100``, which cannot be
74 represented exactly as a float, and rounds to the surprising ``1e100``. For
75 this reason, function :func:`fmod` is generally preferred when working with
76 floats, while Python's ``x % y`` is preferred when working with integers.
77
78
79.. function:: frexp(x)
80
81 Return the mantissa and exponent of *x* as the pair ``(m, e)``. *m* is a float
82 and *e* is an integer such that ``x == m * 2**e`` exactly. If *x* is zero,
83 returns ``(0.0, 0)``, otherwise ``0.5 <= abs(m) < 1``. This is used to "pick
84 apart" the internal representation of a float in a portable way.
85
86
Mark Dickinsonaa7633a2008-08-01 08:16:13 +000087.. function:: fsum(iterable)
88
89 Return an accurate floating point sum of values in the iterable. Avoids
Raymond Hettingerf3936f82009-02-19 05:48:05 +000090 loss of precision by tracking multiple intermediate partial sums::
Mark Dickinsonaa7633a2008-08-01 08:16:13 +000091
Raymond Hettingerf3936f82009-02-19 05:48:05 +000092 >>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
Mark Dickinson5a55b612009-06-28 20:59:42 +000093 0.9999999999999999
Raymond Hettingerf3936f82009-02-19 05:48:05 +000094 >>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
95 1.0
Mark Dickinsonaa7633a2008-08-01 08:16:13 +000096
Raymond Hettingerf3936f82009-02-19 05:48:05 +000097 The algorithm's accuracy depends on IEEE-754 arithmetic guarantees and the
98 typical case where the rounding mode is half-even. On some non-Windows
99 builds, the underlying C library uses extended precision addition and may
100 occasionally double-round an intermediate sum causing it to be off in its
101 least significant bit.
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000102
Raymond Hettinger477be822009-02-19 06:44:30 +0000103 For further discussion and two alternative approaches, see the `ASPN cookbook
104 recipes for accurate floating point summation
Georg Brandl5d941342016-02-26 19:37:12 +0100105 <https://code.activestate.com/recipes/393090/>`_\.
Raymond Hettinger477be822009-02-19 06:44:30 +0000106
Mark Dickinsonaa7633a2008-08-01 08:16:13 +0000107
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +0300108.. function:: gcd(a, b)
109
110 Return the greatest common divisor of the integers *a* and *b*. If either
111 *a* or *b* is nonzero, then the value of ``gcd(a, b)`` is the largest
112 positive integer that divides both *a* and *b*. ``gcd(0, 0)`` returns
113 ``0``.
114
Benjamin Petersone960d182015-05-12 17:24:17 -0400115 .. versionadded:: 3.5
116
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +0300117
Tal Einatd5519ed2015-05-31 22:05:00 +0300118.. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
119
120 Return ``True`` if the values *a* and *b* are close to each other and
121 ``False`` otherwise.
122
123 Whether or not two values are considered close is determined according to
124 given absolute and relative tolerances.
125
126 *rel_tol* is the relative tolerance -- it is the maximum allowed difference
127 between *a* and *b*, relative to the larger absolute value of *a* or *b*.
128 For example, to set a tolerance of 5%, pass ``rel_tol=0.05``. The default
129 tolerance is ``1e-09``, which assures that the two values are the same
130 within about 9 decimal digits. *rel_tol* must be greater than zero.
131
132 *abs_tol* is the minimum absolute tolerance -- useful for comparisons near
133 zero. *abs_tol* must be at least zero.
134
135 If no errors occur, the result will be:
136 ``abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)``.
137
138 The IEEE 754 special values of ``NaN``, ``inf``, and ``-inf`` will be
139 handled according to IEEE rules. Specifically, ``NaN`` is not considered
140 close to any other value, including ``NaN``. ``inf`` and ``-inf`` are only
141 considered close to themselves.
142
143 .. versionadded:: 3.5
144
145 .. seealso::
146
147 :pep:`485` -- A function for testing approximate equality
148
149
Mark Dickinson8e0c9962010-07-11 17:38:24 +0000150.. function:: isfinite(x)
151
152 Return ``True`` if *x* is neither an infinity nor a NaN, and
153 ``False`` otherwise. (Note that ``0.0`` *is* considered finite.)
154
Mark Dickinsonc7622422010-07-11 19:47:37 +0000155 .. versionadded:: 3.2
156
Mark Dickinson8e0c9962010-07-11 17:38:24 +0000157
Christian Heimes072c0f12008-01-03 23:01:04 +0000158.. function:: isinf(x)
159
Mark Dickinsonc7622422010-07-11 19:47:37 +0000160 Return ``True`` if *x* is a positive or negative infinity, and
161 ``False`` otherwise.
Christian Heimes072c0f12008-01-03 23:01:04 +0000162
Christian Heimes072c0f12008-01-03 23:01:04 +0000163
164.. function:: isnan(x)
165
Mark Dickinsonc7622422010-07-11 19:47:37 +0000166 Return ``True`` if *x* is a NaN (not a number), and ``False`` otherwise.
Christian Heimes072c0f12008-01-03 23:01:04 +0000167
Christian Heimes072c0f12008-01-03 23:01:04 +0000168
Mark Dickinson73934b92019-05-18 12:29:50 +0100169.. function:: isqrt(n)
170
171 Return the integer square root of the nonnegative integer *n*. This is the
172 floor of the exact square root of *n*, or equivalently the greatest integer
173 *a* such that *a*\ ² |nbsp| ≤ |nbsp| *n*.
174
175 For some applications, it may be more convenient to have the least integer
176 *a* such that *n* |nbsp| ≤ |nbsp| *a*\ ², or in other words the ceiling of
177 the exact square root of *n*. For positive *n*, this can be computed using
178 ``a = 1 + isqrt(n - 1)``.
179
180 .. versionadded:: 3.8
181
182
Georg Brandl116aa622007-08-15 14:28:22 +0000183.. function:: ldexp(x, i)
184
185 Return ``x * (2**i)``. This is essentially the inverse of function
186 :func:`frexp`.
187
188
189.. function:: modf(x)
190
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000191 Return the fractional and integer parts of *x*. Both results carry the sign
192 of *x* and are floats.
Georg Brandl116aa622007-08-15 14:28:22 +0000193
Christian Heimes400adb02008-02-01 08:12:03 +0000194
Pablo Galindobc098512019-02-07 07:04:02 +0000195.. function:: prod(iterable, *, start=1)
196
197 Calculate the product of all the elements in the input *iterable*.
198 The default *start* value for the product is ``1``.
199
200 When the iterable is empty, return the start value. This function is
201 intended specifically for use with numeric values and may reject
202 non-numeric types.
203
204 .. versionadded:: 3.8
205
206
Mark Dickinsona0ce3752017-04-05 18:34:27 +0100207.. function:: remainder(x, y)
208
209 Return the IEEE 754-style remainder of *x* with respect to *y*. For
210 finite *x* and finite nonzero *y*, this is the difference ``x - n*y``,
211 where ``n`` is the closest integer to the exact value of the quotient ``x /
212 y``. If ``x / y`` is exactly halfway between two consecutive integers, the
213 nearest *even* integer is used for ``n``. The remainder ``r = remainder(x,
214 y)`` thus always satisfies ``abs(r) <= 0.5 * abs(y)``.
215
216 Special cases follow IEEE 754: in particular, ``remainder(x, math.inf)`` is
217 *x* for any finite *x*, and ``remainder(x, 0)`` and
218 ``remainder(math.inf, x)`` raise :exc:`ValueError` for any non-NaN *x*.
219 If the result of the remainder operation is zero, that zero will have
220 the same sign as *x*.
221
222 On platforms using IEEE 754 binary floating-point, the result of this
223 operation is always exactly representable: no rounding error is introduced.
224
225 .. versionadded:: 3.7
226
227
Christian Heimes400adb02008-02-01 08:12:03 +0000228.. function:: trunc(x)
229
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300230 Return the :class:`~numbers.Real` value *x* truncated to an
231 :class:`~numbers.Integral` (usually an integer). Delegates to
Eric Appelt308eab92018-03-10 02:44:12 -0600232 :meth:`x.__trunc__() <object.__trunc__>`.
Christian Heimes400adb02008-02-01 08:12:03 +0000233
Christian Heimes400adb02008-02-01 08:12:03 +0000234
Yash Aggarwal4a686502019-06-01 12:51:27 +0530235.. function:: comb(n, k)
236
237 Return the number of ways to choose *k* items from *n* items without repetition
238 and without order.
239
240 Also called the binomial coefficient. It is mathematically equal to the expression
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +0300241 ``n! / (k! (n - k)!)``. It is equivalent to the coefficient of the *k*-th term in the
Yash Aggarwal4a686502019-06-01 12:51:27 +0530242 polynomial expansion of the expression ``(1 + x) ** n``.
243
244 Raises :exc:`TypeError` if the arguments not integers.
Serhiy Storchaka2b843ac2019-06-01 22:09:02 +0300245 Raises :exc:`ValueError` if the arguments are negative or if *k* > *n*.
Yash Aggarwal4a686502019-06-01 12:51:27 +0530246
247 .. versionadded:: 3.8
248
249
Georg Brandl116aa622007-08-15 14:28:22 +0000250Note that :func:`frexp` and :func:`modf` have a different call/return pattern
251than their C equivalents: they take a single argument and return a pair of
252values, rather than returning their second return value through an 'output
253parameter' (there is no such thing in Python).
254
255For the :func:`ceil`, :func:`floor`, and :func:`modf` functions, note that *all*
256floating-point numbers of sufficiently large magnitude are exact integers.
257Python floats typically carry no more than 53 bits of precision (the same as the
258platform C double type), in which case any float *x* with ``abs(x) >= 2**52``
259necessarily has no fractional bits.
260
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000261
262Power and logarithmic functions
263-------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000264
Georg Brandl116aa622007-08-15 14:28:22 +0000265.. function:: exp(x)
266
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300267 Return *e* raised to the power *x*, where *e* = 2.718281... is the base
268 of natural logarithms. This is usually more accurate than ``math.e ** x``
269 or ``pow(math.e, x)``.
270
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Mark Dickinson664b5112009-12-16 20:23:42 +0000272.. function:: expm1(x)
273
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300274 Return *e* raised to the power *x*, minus 1. Here *e* is the base of natural
275 logarithms. For small floats *x*, the subtraction in ``exp(x) - 1``
Raymond Hettinger1081d482011-03-31 12:04:53 -0700276 can result in a `significant loss of precision
Georg Brandl5d941342016-02-26 19:37:12 +0100277 <https://en.wikipedia.org/wiki/Loss_of_significance>`_\; the :func:`expm1`
Raymond Hettinger1081d482011-03-31 12:04:53 -0700278 function provides a way to compute this quantity to full precision::
Mark Dickinson664b5112009-12-16 20:23:42 +0000279
280 >>> from math import exp, expm1
281 >>> exp(1e-5) - 1 # gives result accurate to 11 places
282 1.0000050000069649e-05
283 >>> expm1(1e-5) # result accurate to full precision
284 1.0000050000166668e-05
285
Mark Dickinson45f992a2009-12-19 11:20:49 +0000286 .. versionadded:: 3.2
287
Mark Dickinson664b5112009-12-16 20:23:42 +0000288
Georg Brandl116aa622007-08-15 14:28:22 +0000289.. function:: log(x[, base])
290
Georg Brandla6053b42009-09-01 08:11:14 +0000291 With one argument, return the natural logarithm of *x* (to base *e*).
292
293 With two arguments, return the logarithm of *x* to the given *base*,
294 calculated as ``log(x)/log(base)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000295
Georg Brandl116aa622007-08-15 14:28:22 +0000296
Christian Heimes53876d92008-04-19 00:31:39 +0000297.. function:: log1p(x)
298
299 Return the natural logarithm of *1+x* (base *e*). The
300 result is calculated in a way which is accurate for *x* near zero.
301
Christian Heimes53876d92008-04-19 00:31:39 +0000302
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200303.. function:: log2(x)
304
Benjamin Petersoneaee1382011-05-08 19:48:08 -0500305 Return the base-2 logarithm of *x*. This is usually more accurate than
306 ``log(x, 2)``.
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200307
308 .. versionadded:: 3.3
309
Victor Stinner9415afc2011-09-21 03:35:18 +0200310 .. seealso::
311
312 :meth:`int.bit_length` returns the number of bits necessary to represent
313 an integer in binary, excluding the sign and leading zeros.
314
Victor Stinnerfa0e3d52011-05-09 01:01:09 +0200315
Georg Brandl116aa622007-08-15 14:28:22 +0000316.. function:: log10(x)
317
Georg Brandla6053b42009-09-01 08:11:14 +0000318 Return the base-10 logarithm of *x*. This is usually more accurate
319 than ``log(x, 10)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000320
321
322.. function:: pow(x, y)
323
Christian Heimesa342c012008-04-20 21:01:16 +0000324 Return ``x`` raised to the power ``y``. Exceptional cases follow
325 Annex 'F' of the C99 standard as far as possible. In particular,
326 ``pow(1.0, x)`` and ``pow(x, 0.0)`` always return ``1.0``, even
327 when ``x`` is a zero or a NaN. If both ``x`` and ``y`` are finite,
328 ``x`` is negative, and ``y`` is not an integer then ``pow(x, y)``
329 is undefined, and raises :exc:`ValueError`.
Christian Heimes53876d92008-04-19 00:31:39 +0000330
Ezio Melotti739d5492013-02-23 04:53:44 +0200331 Unlike the built-in ``**`` operator, :func:`math.pow` converts both
332 its arguments to type :class:`float`. Use ``**`` or the built-in
333 :func:`pow` function for computing exact integer powers.
334
Georg Brandl116aa622007-08-15 14:28:22 +0000335
336.. function:: sqrt(x)
337
338 Return the square root of *x*.
339
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300340
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000341Trigonometric functions
342-----------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000343
Georg Brandl116aa622007-08-15 14:28:22 +0000344.. function:: acos(x)
345
346 Return the arc cosine of *x*, in radians.
347
348
349.. function:: asin(x)
350
351 Return the arc sine of *x*, in radians.
352
353
354.. function:: atan(x)
355
356 Return the arc tangent of *x*, in radians.
357
358
359.. function:: atan2(y, x)
360
361 Return ``atan(y / x)``, in radians. The result is between ``-pi`` and ``pi``.
362 The vector in the plane from the origin to point ``(x, y)`` makes this angle
363 with the positive X axis. The point of :func:`atan2` is that the signs of both
364 inputs are known to it, so it can compute the correct quadrant for the angle.
Mark Dickinson603b7532010-04-06 19:55:03 +0000365 For example, ``atan(1)`` and ``atan2(1, 1)`` are both ``pi/4``, but ``atan2(-1,
Georg Brandl116aa622007-08-15 14:28:22 +0000366 -1)`` is ``-3*pi/4``.
367
368
369.. function:: cos(x)
370
371 Return the cosine of *x* radians.
372
373
Raymond Hettinger9c18b1a2018-07-31 00:45:49 -0700374.. function:: dist(p, q)
375
376 Return the Euclidean distance between two points *p* and *q*, each
377 given as a tuple of coordinates. The two tuples must be the same size.
378
379 Roughly equivalent to::
380
381 sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
382
383 .. versionadded:: 3.8
384
385
Raymond Hettingerc6dabe32018-07-28 07:48:04 -0700386.. function:: hypot(*coordinates)
Georg Brandl116aa622007-08-15 14:28:22 +0000387
Raymond Hettingerc6dabe32018-07-28 07:48:04 -0700388 Return the Euclidean norm, ``sqrt(sum(x**2 for x in coordinates))``.
389 This is the length of the vector from the origin to the point
390 given by the coordinates.
391
392 For a two dimensional point ``(x, y)``, this is equivalent to computing
393 the hypotenuse of a right triangle using the Pythagorean theorem,
394 ``sqrt(x*x + y*y)``.
395
396 .. versionchanged:: 3.8
397 Added support for n-dimensional points. Formerly, only the two
398 dimensional case was supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000399
400
401.. function:: sin(x)
402
403 Return the sine of *x* radians.
404
405
406.. function:: tan(x)
407
408 Return the tangent of *x* radians.
409
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300410
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000411Angular conversion
412------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000413
Georg Brandl116aa622007-08-15 14:28:22 +0000414.. function:: degrees(x)
415
Benjamin Peterson19a3f172015-05-12 19:15:53 -0400416 Convert angle *x* from radians to degrees.
Georg Brandl116aa622007-08-15 14:28:22 +0000417
418
419.. function:: radians(x)
420
Benjamin Peterson19a3f172015-05-12 19:15:53 -0400421 Convert angle *x* from degrees to radians.
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300423
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000424Hyperbolic functions
425--------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000426
Georg Brandl5d941342016-02-26 19:37:12 +0100427`Hyperbolic functions <https://en.wikipedia.org/wiki/Hyperbolic_function>`_
Raymond Hettinger1081d482011-03-31 12:04:53 -0700428are analogs of trigonometric functions that are based on hyperbolas
429instead of circles.
Georg Brandl116aa622007-08-15 14:28:22 +0000430
Christian Heimesa342c012008-04-20 21:01:16 +0000431.. function:: acosh(x)
432
433 Return the inverse hyperbolic cosine of *x*.
434
Christian Heimesa342c012008-04-20 21:01:16 +0000435
436.. function:: asinh(x)
437
438 Return the inverse hyperbolic sine of *x*.
439
Christian Heimesa342c012008-04-20 21:01:16 +0000440
441.. function:: atanh(x)
442
443 Return the inverse hyperbolic tangent of *x*.
444
Christian Heimesa342c012008-04-20 21:01:16 +0000445
Georg Brandl116aa622007-08-15 14:28:22 +0000446.. function:: cosh(x)
447
448 Return the hyperbolic cosine of *x*.
449
450
451.. function:: sinh(x)
452
453 Return the hyperbolic sine of *x*.
454
455
456.. function:: tanh(x)
457
458 Return the hyperbolic tangent of *x*.
459
Christian Heimes53876d92008-04-19 00:31:39 +0000460
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000461Special functions
462-----------------
463
Mark Dickinson45f992a2009-12-19 11:20:49 +0000464.. function:: erf(x)
465
Georg Brandl5d941342016-02-26 19:37:12 +0100466 Return the `error function <https://en.wikipedia.org/wiki/Error_function>`_ at
Raymond Hettinger1081d482011-03-31 12:04:53 -0700467 *x*.
468
469 The :func:`erf` function can be used to compute traditional statistical
470 functions such as the `cumulative standard normal distribution
Georg Brandl5d941342016-02-26 19:37:12 +0100471 <https://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution_function>`_::
Raymond Hettinger1081d482011-03-31 12:04:53 -0700472
473 def phi(x):
474 'Cumulative distribution function for the standard normal distribution'
475 return (1.0 + erf(x / sqrt(2.0))) / 2.0
Mark Dickinson45f992a2009-12-19 11:20:49 +0000476
477 .. versionadded:: 3.2
478
479
480.. function:: erfc(x)
481
Raymond Hettinger1081d482011-03-31 12:04:53 -0700482 Return the complementary error function at *x*. The `complementary error
Georg Brandl5d941342016-02-26 19:37:12 +0100483 function <https://en.wikipedia.org/wiki/Error_function>`_ is defined as
Raymond Hettinger12e6c252011-03-31 13:59:24 -0700484 ``1.0 - erf(x)``. It is used for large values of *x* where a subtraction
485 from one would cause a `loss of significance
Georg Brandl5d941342016-02-26 19:37:12 +0100486 <https://en.wikipedia.org/wiki/Loss_of_significance>`_\.
Mark Dickinson45f992a2009-12-19 11:20:49 +0000487
488 .. versionadded:: 3.2
489
490
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000491.. function:: gamma(x)
492
Georg Brandl5d941342016-02-26 19:37:12 +0100493 Return the `Gamma function <https://en.wikipedia.org/wiki/Gamma_function>`_ at
Raymond Hettinger12e6c252011-03-31 13:59:24 -0700494 *x*.
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000495
Mark Dickinson56e09662009-10-01 16:13:29 +0000496 .. versionadded:: 3.2
Mark Dickinson12c4bdb2009-09-28 19:21:11 +0000497
498
Mark Dickinson05d2e082009-12-11 20:17:17 +0000499.. function:: lgamma(x)
500
501 Return the natural logarithm of the absolute value of the Gamma
502 function at *x*.
503
Mark Dickinson45f992a2009-12-19 11:20:49 +0000504 .. versionadded:: 3.2
Mark Dickinson05d2e082009-12-11 20:17:17 +0000505
506
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000507Constants
Mark Dickinson60fe6b02009-06-02 12:53:15 +0000508---------
Georg Brandl116aa622007-08-15 14:28:22 +0000509
510.. data:: pi
511
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300512 The mathematical constant *π* = 3.141592..., to available precision.
Georg Brandl116aa622007-08-15 14:28:22 +0000513
514
515.. data:: e
516
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300517 The mathematical constant *e* = 2.718281..., to available precision.
518
Georg Brandl116aa622007-08-15 14:28:22 +0000519
Guido van Rossum0a891d72016-08-15 09:12:52 -0700520.. data:: tau
521
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300522 The mathematical constant *τ* = 6.283185..., to available precision.
523 Tau is a circle constant equal to 2\ *π*, the ratio of a circle's circumference to
Guido van Rossum0a891d72016-08-15 09:12:52 -0700524 its radius. To learn more about Tau, check out Vi Hart's video `Pi is (still)
525 Wrong <https://www.youtube.com/watch?v=jG7vhMMXagQ>`_, and start celebrating
Sanyam Khurana338cd832018-01-20 05:55:37 +0530526 `Tau day <https://tauday.com/>`_ by eating twice as much pie!
Christian Heimes53876d92008-04-19 00:31:39 +0000527
Georg Brandl4770d6e2016-08-16 07:08:46 +0200528 .. versionadded:: 3.6
529
Serhiy Storchakadbaf7462017-05-04 12:25:09 +0300530
Mark Dickinsona5d0c7c2015-01-11 11:55:29 +0000531.. data:: inf
532
533 A floating-point positive infinity. (For negative infinity, use
534 ``-math.inf``.) Equivalent to the output of ``float('inf')``.
535
536 .. versionadded:: 3.5
537
538
539.. data:: nan
540
541 A floating-point "not a number" (NaN) value. Equivalent to the output of
542 ``float('nan')``.
543
544 .. versionadded:: 3.5
545
546
Georg Brandl495f7b52009-10-27 15:28:25 +0000547.. impl-detail::
Georg Brandl116aa622007-08-15 14:28:22 +0000548
549 The :mod:`math` module consists mostly of thin wrappers around the platform C
Mark Dickinson603b7532010-04-06 19:55:03 +0000550 math library functions. Behavior in exceptional cases follows Annex F of
551 the C99 standard where appropriate. The current implementation will raise
552 :exc:`ValueError` for invalid operations like ``sqrt(-1.0)`` or ``log(0.0)``
553 (where C99 Annex F recommends signaling invalid operation or divide-by-zero),
554 and :exc:`OverflowError` for results that overflow (for example,
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000555 ``exp(1000.0)``). A NaN will not be returned from any of the functions
556 above unless one or more of the input arguments was a NaN; in that case,
557 most functions will return a NaN, but (again following C99 Annex F) there
Mark Dickinson603b7532010-04-06 19:55:03 +0000558 are some exceptions to this rule, for example ``pow(float('nan'), 0.0)`` or
559 ``hypot(float('nan'), float('inf'))``.
Georg Brandl116aa622007-08-15 14:28:22 +0000560
Mark Dickinson42dfeec2010-04-06 22:13:37 +0000561 Note that Python makes no effort to distinguish signaling NaNs from
562 quiet NaNs, and behavior for signaling NaNs remains unspecified.
563 Typical behavior is to treat all NaNs as though they were quiet.
Christian Heimes53876d92008-04-19 00:31:39 +0000564
Georg Brandl116aa622007-08-15 14:28:22 +0000565
566.. seealso::
567
568 Module :mod:`cmath`
569 Complex number versions of many of these functions.
Mark Dickinson73934b92019-05-18 12:29:50 +0100570
571.. |nbsp| unicode:: 0xA0
572 :trim: