Python math.tanh() Method

Python math.tanh() Method

  1. Syntax of Python math.tanh() Method
  2. Example 1: Use the math.tanh() Method in Python
  3. Example 2: Use the math.tanh() Method to Get the Hyperbolic Tangent of Degrees
  4. Example 3: TypeError When Using math.tanh() Method

Python math.tanh() method is an efficient way of calculating the hyperbolic tangent of a number in radians. The input parameter must be in radians.

Syntax of Python math.tanh() Method

math.tanh(x)

Parameters

x - Any positive or negative radian value to be operated on.

Return

This method returns a float value representing the hyperbolic tangent of x in radians.

Example 1: Use the math.tanh() Method in Python

import math

x=90

value=math.tanh(x)

print(f"The hyperbolic tangent of {x} is {value}.")

x=0

value=math.tanh(x)

print(f"The hyperbolic tangent of {x} is {value}.")

x=-34.5

value=math.tanh(x)

print(f"The hyperbolic tangent of {x} is {value}.")

x=math.pi

value=math.tanh(x)

print(f"The hyperbolic tangent of {x} is {value}.")

x=math.inf

value=math.tanh(x)

print(f"The hyperbolic tangent of {x} is {value}.")

Output:

The hyperbolic tangent of 90 is 1.0.
The hyperbolic tangent of 0 is 0.0.
The hyperbolic tangent of -34.5 is -1.0.
The hyperbolic tangent of 3.141592653589793 is 0.99627207622075.
The hyperbolic tangent of inf is 1.0.

Note that the arguments can be in integers or floats. The values may either be positive or negative.

Example 2: Use the math.tanh() Method to Get the Hyperbolic Tangent of Degrees

import math

print("The hyperbolic tangent of 30 degrees is ", math.tanh(math.radians(30)))

Output:

The hyperbolic tangent of 30 degrees is  0.4804727781564516

We use these methods in mathematical computations related to geometry and have a certain application in astronomical computations.

Example 3: TypeError When Using math.tanh() Method

import math

x='h'

value=math.tanh(x)

print(f"The hyperbolic tangent of {x} is {value}.")

Output:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    value=math.tan(x)
TypeError: must be real number, not str

Note that a TypeError exception may occur if the argument value is not a number.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python Math

  • Python math.pow() Method
  • Python Math.erf() Method
  • Python Math.erfc() Method
  • Python Math.expm1() Method
  • Python Math.fabs() Method
  • Python Math.factorial() Method