import math
print(math.tanh(-5)) # -0.9999092042625951
print(math.tanh(-1)) # -0.7615941559557649
print(math.tanh(-0.9)) # -0.7162978701990244
print(math.tanh(0)) # 0.0
print(math.tanh(0.9)) # 0.7162978701990244
print(math.tanh(1)) # 0.7615941559557649
print(math.tanh(5)) # 0.9999092042625951
Note that all the inputs are in radian. import math
in_degree = 50
in_redian = math.radians(in_degree)
print(math.tanh(in_redian)) # 0.7027254303610717
import matplotlib.pyplot as plt
x=[]
y=[]
i=-10
while (i<=10):
x.append(i)
y.append(math.tanh(i))
i=i+0.1
plt.plot(x,y)
plt.axvline(x=0.00,linewidth=2, color='#f1f1f1')
plt.axhline(y=0.00,linewidth=2, color='#f1f1f1')
plt.grid(linestyle='-',
linewidth=0.5,color='#f1f1f1')
plt.show()
import math
inputs = [-2, -1, 0, 1, 2]
outputs = [math.tanh(x) for x in inputs]
print(outputs) # Outputs between -1 and 1
import cmath
z = 2 + 3j
print(cmath.tanh(z)) # Outputs tanh for a complex number
Output
(0.965385879022133-0.009884375038322495j)
import math
x = 1.0
tanh_val = math.tanh(x)
sinh_cosh_val = math.sinh(x) / math.cosh(x)
print(f"tanh(1.0) = {tanh_val}")
print(f"sinh(1.0) / cosh(1.0) = {sinh_cosh_val}")
Output
tanh(1.0) = 0.7615941559557649
sinh(1.0) / cosh(1.0) = 0.7615941559557649
import math
x = 100 # Large input
print(math.tanh(x)) # Output will approach 1 as x increases
print(math.tanh(-100)) # Output will approach -1 as x decreases
import math
x = 0.5
y = 2.0
result = 3 * math.tanh(x) + 2 * math.tanh(y)
print(result) # output: 3.314406631931663
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.