Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Math.Tanh() Method in C#
The Math.Tanh() method in C# returns the hyperbolic tangent of a specified angle. The hyperbolic tangent function is commonly used in mathematical calculations, especially in calculus and engineering applications. Unlike the regular tangent function which deals with circular trigonometry, the hyperbolic tangent operates on hyperbolic angles.
Syntax
Following is the syntax for the Math.Tanh() method −
public static double Tanh(double value);
Parameters
The method accepts the following parameter −
-
value − A
doublerepresenting the angle in radians for which to calculate the hyperbolic tangent.
Return Value
The method returns a double value representing the hyperbolic tangent of the specified angle. The returned value is always between -1 and 1.
Using Math.Tanh() with Different Values
Example
using System;
public class Demo {
public static void Main(){
double val1 = 30.0;
double val2 = (60 * (Math.PI)) / 180;
Console.WriteLine("Tanh(30.0): " + Math.Tanh(val1));
Console.WriteLine("Tanh(60 degrees in radians): " + Math.Tanh(val2));
}
}
The output of the above code is −
Tanh(30.0): 1 Tanh(60 degrees in radians): 0.780714435359268
Using Math.Tanh() with Special Values
Example
using System;
public class Demo {
public static void Main(){
double val1 = 0.0;
double val2 = 1.0;
double val3 = 15.0;
Console.WriteLine("Tanh(0.0): " + Math.Tanh(val1));
Console.WriteLine("Tanh(1.0): " + Math.Tanh(val2));
Console.WriteLine("Tanh(15.0): " + Math.Tanh(val3));
}
}
The output of the above code is −
Tanh(0.0): 0 Tanh(1.0): 0.761594155955765 Tanh(15.0): 0.999999999999813
Using Math.Tanh() with Negative Values
Example
using System;
public class Demo {
public static void Main(){
double val1 = -2.0;
double val2 = -0.5;
double val3 = 2.0;
Console.WriteLine("Tanh(-2.0): " + Math.Tanh(val1));
Console.WriteLine("Tanh(-0.5): " + Math.Tanh(val2));
Console.WriteLine("Tanh(2.0): " + Math.Tanh(val3));
Console.WriteLine("Notice the symmetry: tanh(-x) = -tanh(x)");
}
}
The output of the above code is −
Tanh(-2.0): -0.964027580075817 Tanh(-0.5): -0.462117157260009 Tanh(2.0): 0.964027580075817 Notice the symmetry: tanh(-x) = -tanh(x)
Key Properties of Math.Tanh()
-
The hyperbolic tangent function is odd, meaning
tanh(-x) = -tanh(x). -
The range of
Math.Tanh()is between -1 and 1 (exclusive of the bounds). -
As the input approaches positive infinity, the result approaches 1.
-
As the input approaches negative infinity, the result approaches -1.
-
Math.Tanh(0)always returns 0.
Conclusion
The Math.Tanh() method calculates the hyperbolic tangent of an angle in radians, returning values between -1 and 1. It is particularly useful in mathematical computations involving hyperbolic functions and is commonly used in machine learning activation functions and engineering calculations.
