Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python - cmath.exp() Function



Thecmath.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 returnsex wheree is the base of natural logarithms.

This method is part of thecmath module, which provides mathematical functions for complex numbers.

Syntax

Following is the basic syntax of the Pythoncmath.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 Pythoncmath.exp() function. Here we are trying to calculate the exponential value of a simple complex number (ecompex_number) −

import cmathz = 1+2jresult = 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 of2

#Example with a real number −import cmathreal_num = 2result = 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 cmathz = "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 thecmath.exp() method −

import cmathx = -96.009result = 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 piresult = cmath.exp(cmath.pi)print(result)#Exponential value of eresult = cmath.exp(cmath.e)print(result)

Output

The output obtained is as follows −

(23.140692632779267+0j)(15.154262241479262+0j)
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp