| 
									
										
										
										
											2024-05-08 15:34:40 -04:00
										 |  |  | :mod:`!cmath` --- Mathematical functions for complex numbers
 | 
					
						
							|  |  |  | ============================================================
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. module:: cmath
 | 
					
						
							|  |  |  |    :synopsis: Mathematical functions for complex numbers.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 15:02:54 -04:00
										 |  |  | --------------
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-17 05:59:14 -04:00
										 |  |  | This module provides access to mathematical functions for complex numbers.  The
 | 
					
						
							|  |  |  | functions in this module accept integers, floating-point numbers or complex
 | 
					
						
							|  |  |  | numbers as arguments. They will also accept any Python object that has either a
 | 
					
						
							| 
									
										
										
										
											2023-06-30 23:27:09 +09:00
										 |  |  | :meth:`~object.__complex__` or a :meth:`~object.__float__` method: these methods are used to
 | 
					
						
							| 
									
										
										
										
											2019-05-17 05:59:14 -04:00
										 |  |  | convert the object to a complex or floating-point number, respectively, and
 | 
					
						
							|  |  |  | the function is then applied to the result of the conversion.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | .. note::
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |    For functions involving branch cuts, we have the problem of deciding how to
 | 
					
						
							|  |  |  |    define those functions on the cut itself. Following Kahan's "Branch cuts for
 | 
					
						
							|  |  |  |    complex elementary functions" paper, as well as Annex G of C99 and later C
 | 
					
						
							|  |  |  |    standards, we use the sign of zero to distinguish one side of the branch cut
 | 
					
						
							|  |  |  |    from the other: for a branch cut along (a portion of) the real axis we look
 | 
					
						
							|  |  |  |    at the sign of the imaginary part, while for a branch cut along the
 | 
					
						
							|  |  |  |    imaginary axis we look at the sign of the real part.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    For example, the :func:`cmath.sqrt` function has a branch cut along the
 | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |    negative real axis. An argument of ``-2-0j`` is treated as
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |    though it lies *below* the branch cut, and so gives a result on the negative
 | 
					
						
							|  |  |  |    imaginary axis::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |       >>> cmath.sqrt(-2-0j)
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |       -1.4142135623730951j
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |    But an argument of ``-2+0j`` is treated as though it lies above
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |    the branch cut::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |       >>> cmath.sqrt(-2+0j)
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |       1.4142135623730951j
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | Conversions to and from polar coordinates
 | 
					
						
							|  |  |  | -----------------------------------------
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | A Python complex number ``z`` is stored internally using *rectangular*
 | 
					
						
							|  |  |  | or *Cartesian* coordinates.  It is completely determined by its *real
 | 
					
						
							| 
									
										
										
										
											2024-05-30 23:20:07 +03:00
										 |  |  | part* ``z.real`` and its *imaginary part* ``z.imag``.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | *Polar coordinates* give an alternative way to represent a complex
 | 
					
						
							|  |  |  | number.  In polar coordinates, a complex number *z* is defined by the
 | 
					
						
							|  |  |  | modulus *r* and the phase angle *phi*. The modulus *r* is the distance
 | 
					
						
							|  |  |  | from *z* to the origin, while the phase *phi* is the counterclockwise
 | 
					
						
							| 
									
										
										
										
											2010-01-02 14:33:10 +00:00
										 |  |  | angle, measured in radians, from the positive x-axis to the line
 | 
					
						
							|  |  |  | segment that joins the origin to *z*.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | The following functions can be used to convert from the native
 | 
					
						
							|  |  |  | rectangular coordinates to polar coordinates and back.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. function:: phase(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |    Return the phase of *x* (also known as the *argument* of *x*), as a float.
 | 
					
						
							|  |  |  |    ``phase(x)`` is equivalent to ``math.atan2(x.imag, x.real)``.  The result
 | 
					
						
							|  |  |  |    lies in the range [-\ *Ï€*, *Ï€*], and the branch cut for this operation lies
 | 
					
						
							|  |  |  |    along the negative real axis.  The sign of the result is the same as the
 | 
					
						
							|  |  |  |    sign of ``x.imag``, even when ``x.imag`` is zero::
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |       >>> phase(-1+0j)
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |       3.141592653589793
 | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |       >>> phase(-1-0j)
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |       -3.141592653589793
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. note::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    The modulus (absolute value) of a complex number *x* can be
 | 
					
						
							|  |  |  |    computed using the built-in :func:`abs` function.  There is no
 | 
					
						
							|  |  |  |    separate :mod:`cmath` module function for this operation.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: polar(x)
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    Return the representation of *x* in polar coordinates.  Returns a
 | 
					
						
							|  |  |  |    pair ``(r, phi)`` where *r* is the modulus of *x* and phi is the
 | 
					
						
							|  |  |  |    phase of *x*.  ``polar(x)`` is equivalent to ``(abs(x),
 | 
					
						
							|  |  |  |    phase(x))``.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: rect(r, phi)
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    Return the complex number *x* with polar coordinates *r* and *phi*.
 | 
					
						
							| 
									
										
										
										
											2024-05-30 23:20:07 +03:00
										 |  |  |    Equivalent to ``complex(r * math.cos(phi), r * math.sin(phi))``.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | Power and logarithmic functions
 | 
					
						
							|  |  |  | -------------------------------
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: exp(x)
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-04 12:25:09 +03:00
										 |  |  |    Return *e* raised to the power *x*, where *e* is the base of natural
 | 
					
						
							|  |  |  |    logarithms.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-05 16:36:33 +00:00
										 |  |  | .. function:: log(x[, base])
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    Returns the logarithm of *x* to the given *base*. If the *base* is not
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |    specified, returns the natural logarithm of *x*. There is one branch cut,
 | 
					
						
							|  |  |  |    from 0 along the negative real axis to -∞.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: log10(x)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    Return the base-10 logarithm of *x*. This has the same branch cut as
 | 
					
						
							|  |  |  |    :func:`log`.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: sqrt(x)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    Return the square root of *x*. This has the same branch cut as :func:`log`.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | Trigonometric functions
 | 
					
						
							|  |  |  | -----------------------
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: acos(x)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |    Return the arc cosine of *x*. There are two branch cuts: One extends right
 | 
					
						
							|  |  |  |    from 1 along the real axis to ∞. The other extends left from -1 along the
 | 
					
						
							|  |  |  |    real axis to -∞.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: asin(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Return the arc sine of *x*. This has the same branch cuts as :func:`acos`.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. function:: atan(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Return the arc tangent of *x*. There are two branch cuts: One extends from
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |    ``1j`` along the imaginary axis to ``∞j``. The other extends from ``-1j``
 | 
					
						
							|  |  |  |    along the imaginary axis to ``-∞j``.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. function:: cos(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Return the cosine of *x*.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: sin(x)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    Return the sine of *x*.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: tan(x)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    Return the tangent of *x*.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | Hyperbolic functions
 | 
					
						
							|  |  |  | --------------------
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: acosh(x)
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-11 13:08:05 +00:00
										 |  |  |    Return the inverse hyperbolic cosine of *x*. There is one branch cut,
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |    extending left from 1 along the real axis to -∞.
 | 
					
						
							| 
									
										
										
										
											2008-04-19 00:31:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: asinh(x)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-11 13:08:05 +00:00
										 |  |  |    Return the inverse hyperbolic sine of *x*. There are two branch cuts:
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |    One extends from ``1j`` along the imaginary axis to ``∞j``.  The other
 | 
					
						
							|  |  |  |    extends from ``-1j`` along the imaginary axis to ``-∞j``.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: atanh(x)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-11 13:08:05 +00:00
										 |  |  |    Return the inverse hyperbolic tangent of *x*. There are two branch cuts: One
 | 
					
						
							| 
									
										
										
										
											2023-02-19 19:15:44 +00:00
										 |  |  |    extends from ``1`` along the real axis to ``∞``. The other extends from
 | 
					
						
							|  |  |  |    ``-1`` along the real axis to ``-∞``.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: cosh(x)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    Return the hyperbolic cosine of *x*.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. function:: sinh(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Return the hyperbolic sine of *x*.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: tanh(x)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    Return the hyperbolic tangent of *x*.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | Classification functions
 | 
					
						
							|  |  |  | ------------------------
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-11 17:38:24 +00:00
										 |  |  | .. function:: isfinite(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-11 19:47:37 +00:00
										 |  |  |    Return ``True`` if both the real and imaginary parts of *x* are finite, and
 | 
					
						
							|  |  |  |    ``False`` otherwise.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 3.2
 | 
					
						
							| 
									
										
										
										
											2010-07-11 17:38:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: isinf(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-11 19:47:37 +00:00
										 |  |  |    Return ``True`` if either the real or the imaginary part of *x* is an
 | 
					
						
							|  |  |  |    infinity, and ``False`` otherwise.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | .. function:: isnan(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-11 19:47:37 +00:00
										 |  |  |    Return ``True`` if either the real or the imaginary part of *x* is a NaN,
 | 
					
						
							|  |  |  |    and ``False`` otherwise.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-31 22:05:00 +03:00
										 |  |  | .. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Return ``True`` if the values *a* and *b* are close to each other and
 | 
					
						
							|  |  |  |    ``False`` otherwise.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Whether or not two values are considered close is determined according to
 | 
					
						
							| 
									
										
										
										
											2024-11-04 12:08:15 +08:00
										 |  |  |    given absolute and relative tolerances.  If no errors occur, the result will
 | 
					
						
							|  |  |  |    be: ``abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)``.
 | 
					
						
							| 
									
										
										
										
											2015-05-31 22:05:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |    *rel_tol* is the relative tolerance -- it is the maximum allowed difference
 | 
					
						
							|  |  |  |    between *a* and *b*, relative to the larger absolute value of *a* or *b*.
 | 
					
						
							|  |  |  |    For example, to set a tolerance of 5%, pass ``rel_tol=0.05``.  The default
 | 
					
						
							|  |  |  |    tolerance is ``1e-09``, which assures that the two values are the same
 | 
					
						
							| 
									
										
										
										
											2024-11-04 12:08:15 +08:00
										 |  |  |    within about 9 decimal digits.  *rel_tol* must be nonnegative and less
 | 
					
						
							|  |  |  |    than ``1.0``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    *abs_tol* is the absolute tolerance; it defaults to ``0.0`` and it must be
 | 
					
						
							|  |  |  |    nonnegative.  When comparing ``x`` to ``0.0``, ``isclose(x, 0)`` is computed
 | 
					
						
							|  |  |  |    as ``abs(x) <= rel_tol  * abs(x)``, which is ``False`` for any ``x`` and
 | 
					
						
							|  |  |  |    rel_tol less than ``1.0``.  So add an appropriate positive abs_tol argument
 | 
					
						
							|  |  |  |    to the call.
 | 
					
						
							| 
									
										
										
										
											2015-05-31 22:05:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |    The IEEE 754 special values of ``NaN``, ``inf``, and ``-inf`` will be
 | 
					
						
							|  |  |  |    handled according to IEEE rules.  Specifically, ``NaN`` is not considered
 | 
					
						
							|  |  |  |    close to any other value, including ``NaN``.  ``inf`` and ``-inf`` are only
 | 
					
						
							|  |  |  |    considered close to themselves.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 3.5
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. seealso::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       :pep:`485` -- A function for testing approximate equality
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  | Constants
 | 
					
						
							|  |  |  | ---------
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. data:: pi
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 16:31:03 +00:00
										 |  |  |    The mathematical constant *Ï€*, as a float.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: e
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The mathematical constant *e*, as a float.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-04 12:25:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 09:12:52 -07:00
										 |  |  | .. data:: tau
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The mathematical constant *Ï„*, as a float.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-16 07:08:46 +02:00
										 |  |  |    .. versionadded:: 3.6
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-04 12:25:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 13:56:58 +01:00
										 |  |  | .. data:: inf
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Floating-point positive infinity. Equivalent to ``float('inf')``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 3.6
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-04 12:25:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 13:56:58 +01:00
										 |  |  | .. data:: infj
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Complex number with zero real part and positive infinity imaginary
 | 
					
						
							|  |  |  |    part. Equivalent to ``complex(0.0, float('inf'))``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 3.6
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-04 12:25:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 13:56:58 +01:00
										 |  |  | .. data:: nan
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    A floating-point "not a number" (NaN) value.  Equivalent to
 | 
					
						
							|  |  |  |    ``float('nan')``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 3.6
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-04 12:25:09 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 13:56:58 +01:00
										 |  |  | .. data:: nanj
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Complex number with zero real part and NaN imaginary part. Equivalent to
 | 
					
						
							|  |  |  |    ``complex(0.0, float('nan'))``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 3.6
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 09:17:12 +01:00
										 |  |  | .. index:: pair: module; math
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Note that the selection of functions is similar, but not identical, to that in
 | 
					
						
							|  |  |  | module :mod:`math`.  The reason for having two modules is that some users aren't
 | 
					
						
							|  |  |  | interested in complex numbers, and perhaps don't even know what they are.  They
 | 
					
						
							|  |  |  | would rather have ``math.sqrt(-1)`` raise an exception than return a complex
 | 
					
						
							|  |  |  | number. Also note that the functions defined in :mod:`cmath` always return a
 | 
					
						
							|  |  |  | complex number, even if the answer can be expressed as a real number (in which
 | 
					
						
							|  |  |  | case the complex number has an imaginary part of zero).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A note on branch cuts: They are curves along which the given function fails to
 | 
					
						
							|  |  |  | be continuous.  They are a necessary feature of many complex functions.  It is
 | 
					
						
							|  |  |  | assumed that if you need to compute with complex functions, you will understand
 | 
					
						
							|  |  |  | about branch cuts.  Consult almost any (not too elementary) book on complex
 | 
					
						
							|  |  |  | variables for enlightenment.  For information of the proper choice of branch
 | 
					
						
							|  |  |  | cuts for numerical purposes, a good reference should be the following:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. seealso::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Kahan, W:  Branch cuts for complex elementary functions; or, Much ado about
 | 
					
						
							|  |  |  |    nothing's sign bit.  In Iserles, A., and Powell, M. (eds.), The state of the art
 | 
					
						
							| 
									
										
										
										
											2016-11-26 13:43:28 +02:00
										 |  |  |    in numerical analysis. Clarendon Press (1987) pp165--211.
 |