Python - cmath.exp() Function



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.

Syntax

Following 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 Value

This method returns the exponential of the given complex number.

Example 1

Following 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 str

Example 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)
Advertisements