Home » Python

math.tanh() method with example in Python

Python math.tanh() method: Here, we are going to learn about the math.tanh() method with example in Python.
Submitted by IncludeHelp, on April 25, 2019

Python math.tanh() method

math.tanh() method is a library method of math module, it is used to get the hyperbolic tangent of given number in radians, it accepts a number and returns hyperbolic tangent.

Note: math.tanh() method accepts only numbers, if we provide anything else except the number, it returns error TypeError"TypeError: a float is required".

Syntax of math.tanh() method:

    math.tanh(x)

Parameter(s): x – is the number whose hyperbolic tangent to be calculated.

Return value: float – it returns a float value that is the hyperbolic tangent value of the number x.

Example:

    Input:
    x = 1.5

    # function call
    print(math.tanh(x))

    Output:
    0.9051482536448664

Python code to demonstrate example of math.tanh() method

# Python code to demonstrate example of 
# math.tanh() method

# importing math module
import math 

# number 
x = -10
print("math.tanh(",x,"): ", math.tanh(x))

x = 0
print("math.tanh(",x,"): ", math.tanh(x))

x = 1.5
print("math.tanh(",x,"): ", math.tanh(x))

x = 5
print("math.tanh(",x,"): ", math.tanh(x))

x = 15.45
print("math.tanh(",x,"): ", math.tanh(x))

Output

math.tanh( -10 ):  -0.9999999958776927
math.tanh( 0 ):  0.0
math.tanh( 1.5 ):  0.9051482536448664
math.tanh( 5 ):  0.9999092042625951
math.tanh( 15.45 ):  0.999999999999924

TypeError example

# Python code to demonstrate example of 
# math.tanh() method with exception

# importing math module
import math 

# number 
x = "2.5"
print("math.tanh(",x,"): ", math.tanh(x))

Output

Traceback (most recent call last):
  File "/home/main.py", line 9, in <module>
    print("math.tanh(",x,"): ", math.tanh(x))
TypeError: a float is required


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.