cmath.tanh() Method in Python:
The cmath.tanh() method returns the complex number’s hyperbolic tangent.
Syntax:
cmath.tanh(x)
Parameters
x: This is Required. It is a number that will be used to calculate the hyperbolic tangent.
If the value is not a number, a TypeError is returned.
Return Value:
Returns a complex value that represents a complex number’s hyperbolic tangent.
Examples:
Example1:
Input:
Given Complex Number = 3+4j
Output:
The given complex number's (3+4j) hyperbolic tangent value = (1.000709536067233+0.00490825806749606j)
Example2:
Input:
Given realpart = 5 Given imaginary part = 2
Output:
The given complex number's (5+2j) hyperbolic tangent value = (1.0000593501490003-6.872163880119276e-05j)
Note: The above input format is for dynamic input.
cmath.tanh() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import cmath module(for complex number operations) using the import keyword.
- Give the complex number as static input and store it in a variable.
- Pass the given complex number as an argument to the cmath.tanh() method that returns the given complex number’s hyperbolic tangent value.
- Store it in another variable.
- Print the hyperbolic tangent value of the given complex number.
- The Exit of the Program.
Below is the implementation:
# Import cmath module(for complex number operations) using the import keyword. import cmath # Give the complex number as static input and store it in a variable. complexnumb = 3+4j # Pass the given complex number as an argument to the cmath.tanh() method that # returns the the given complex number's hyperbolic tangent value. # Store it in another variable. rslt = cmath.tanh(complexnumb) # Print the hyperbolic tangent value of the given complex number. print("The given complex number's", complexnumb, "hyperbolic tangent value = ") print(rslt)
Output:
The given complex number's (3+4j) hyperbolic tangent value = (1.000709536067233+0.00490825806749606j)
Similarly, try for the other examples
import cmath complexnumb = -2-1j rslt = cmath.tanh(complexnumb) print("The given complex number's", complexnumb, "hyperbolic tangent value = ") print(rslt)
Output:
The given complex number's (-2-1j) hyperbolic tangent value = (-1.0147936161466335-0.0338128260798967j)
Method #2: Using Built-in Functions (User Input)
Approach:
- Import cmath module(for complex number operations) using the import keyword.
- Give the real part and imaginary part of the complex number as user input using map(), int(), split().
- Store it in two variables.
- Using a complex() function convert those two variables into a complex number and store them in a variable.
- Pass the given complex number as an argument to the cmath.tanh() method that returns the given complex number’s hyperbolic tangent value.
- Store it in another variable.
- Print the hyperbolic tangent value of the given complex number.
- The Exit of the Program.
Below is the implementation:
# Import cmath module(for complex number operations) using the import keyword. import cmath # Give the real part and imaginary part of the complex number as user input # using map(), int(), split(). # Store it in two variables. realnumb, imaginarynumb = map(int, input( 'Enter real part and complex part of the complex number = ').split()) # Using a complex() function convert those two variables into a complex number. complexnumb = complex(realnumb, imaginarynumb) # Pass the given complex number as an argument to the cmath.tanh() method that # returns the the given complex number's hyperbolic tangent value. # Store it in another variable. rslt = cmath.tanh(complexnumb) # Print the hyperbolic tangent value of the given complex number. print("The given complex number's", complexnumb, "hyperbolic tangent value = ") print(rslt)
Output:
Enter real part and complex part of the complex number = 5 2 The given complex number's (5+2j) hyperbolic tangent value = (1.0000593501490003-6.872163880119276e-05j)