The builtin cmath
(complex math) module provides access to mathematical functions for complex numbers.
The popular math module contains functions for performing basic math operations with real numbers. However, the module does not support operations on complex numbers. The cmath
module provides additional functions for working with complex numbers, as well as converting between real and complex representations.
The Functions in the cmath
module are mostly made to work primarily with complex numbers and performing any operations with them almost always return a complex number. If the return value can be expressed as a real number, the return value will have an imaginary part of 0j.
Functions In The cmath Module
Function | Description | |
---|---|---|
acos(x) | Returns the arc cosine value of x | |
acosh(x) | Returns the hyperbolic arc cosine of x | |
asin(x) | Returns the arc sine of x | |
asinh() | Returns the hyperbolic arc sine of x | |
atan(x) | Returns the arc tangent value of x | |
atanh(x) | Returns the hyperbolic arctangent value of x | |
cos(x) | Returns the cosine of x | |
cosh(x) | Returns the hyperbolic cosine of x | |
exp(x) | Returns the value of ex, where e is Euler's number (approximately 2.718281...). | |
isclose(x, y) | Checks whether the values of x and y are close, or not | |
isfinite(x) | Checks whether x is a finite number | |
isinf(x) | Check whether x is a positive or negative infinty | |
isnan(x) | Checks whether x is NaN (not a number) | |
log(x[, base]) | Returns the logarithm of x to the base. The base defaults to e. | |
log10(x[,base]) | Returns the base-10 logarithm of x | |
phase() | Return the phase of a complex number | |
polar() | Convert a complex number to polar coordinates | |
rect() | Convert polar coordinates to rectangular form | |
sin(x) | Returns the sine of x | |
sinh(x) | Returns the hyperbolic sine of x | |
sqrt(x) | Returns the square root of x | |
tan(x) | Returns the tangent of x | |
tanh(x) |
|
Constants In The cmath Module
Constant | Description |
---|---|
e | Returns Euler's number (2.7182...) |
inf | Returns a floating-point positive infinity value |
infj | Returns a complex infinity value |
nan | Returns floating-point NaN (Not a Number) value |
nanj | Returns coplext NaN (Not a Number) value |
pi | Returns the value of PI i.e 3.1415... |
tau | Returns tau (6.2831...) |
To use any function or constant defined in the cmath module, you first import the module.
import cmath
print(cmath.e)
print(cmath.isfinite(1+2j))
You can alternatively, import only the function(s) or the constant(s) you need.
from cmath import pi, sin
print(pi)
print(sin(3+2j))