The cmath.exp() function in Python is used to get the exponential value of a complex number. This method accepts a complex number as a parameter and returns the exponential value.
If the given number is x, this function returns ex where e is the base of natural logarithms.
This method is part of the cmath module, which provides mathematical functions for complex numbers.
SyntaxFollowing is the basic syntax of the Python cmath.exp() method −
cmath.exp(z)Parameters
This method accepts a single parameter representing a complex number (or, interpreted as a complex number).
Return ValueThis method returns the exponential of the given complex number.
Example 1Following is the basic example of the Python cmath.exp() function. Here we are trying to calculate the exponential value of a simple complex number (ecompex_number) −
import cmath z = 1+2j result = cmath.exp(z) print(result)Output
The output obtained is as follows −
(-1.13120438375678135+2.2471266720048188j)Example 2
We can also calculate the exponential value of a real number using this function ( ereal_number). In the following example we have calculated exponential value of 2 −
#Example with a real number − import cmath real_num = 2 result = cmath.exp(real_num) print(result)Output
The output obtained is as follows −
(7.38905679873065+0j)Example 3
If the value passed as a parameter to this function is not a real number, it raises an exception. In this example below, we are passing a string value as a parameter to this function −
import cmath z = "invalid" result = cmath.exp(z) print(result)Output
The output obtained is as follows −
Traceback (most recent call last): File "/home/cg/root/91797/main.py", line 3, in result = cmath.exp(z) TypeError: must be real number, not strExample 4
In the following example we are trying to calculate the exponential value of a negative integer using the cmath.exp() method −
import cmath x = -96.009 result = cmath.exp(x) print(result)Output
The output obtained is as follows −
(2.0128948417995313e-42+0j)Example 5
Now, lets us find out the exponential values of the Python predefined constants −
import cmath #Exponential value of pi result = cmath.exp(cmath.pi) print(result) #Exponential value of e result = cmath.exp(cmath.e) print(result)Output
The output obtained is as follows −
(23.140692632779267+0j) (15.154262241479262+0j)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4