C# Tutorial C# Advanced C# References

C# Math - Tanh() Method



The C# Tanh() method returns hyperbolic tangent of a value. The hyperbolic tangent of x is defined as:

Tanh

where e is an Euler's number.

In special cases it returns the following:

  • If the argument is NaN, the method returns NaN.
  • If the argument is positive infinity, the method returns 1.
  • If the argument is negative infinity, the method returns -1.

Syntax

public static double Tanh(double arg);

Parameters

arg Specify the value.

Return Value

Returns the hyperbolic tangent of a value.

Example:

In the example below, Tanh() method is used to find out the hyperbolic tangent of a value.

using System;

class MyProgram {
  static void Main(string[] args) {
    Console.WriteLine("Math.Tanh(-2) = "
                      + Math.Tanh(-2));
    Console.WriteLine("Math.Tanh(-1) = "
                      + Math.Tanh(-1));
    Console.WriteLine("Math.Tanh(0) = "
                      + Math.Tanh(0));
    Console.WriteLine("Math.Tanh(1) = "
                      + Math.Tanh(1));
    Console.WriteLine("Math.Tanh(2) = "
                      + Math.Tanh(2));
    Console.WriteLine("Math.Tanh(Double.NaN) = "
                      + Math.Tanh(Double.NaN));
    Console.WriteLine("Math.Tanh(Double.NegativeInfinity) = "
                      + Math.Tanh(Double.NegativeInfinity));
    Console.WriteLine("Math.Tanh(Double.PositiveInfinity) = "
                      + Math.Tanh(Double.PositiveInfinity));
  }
}

The output of the above code will be:

Math.Tanh(-2) = -0.964027580075817
Math.Tanh(-1) = -0.761594155955765
Math.Tanh(0) = 0
Math.Tanh(1) = 0.761594155955765
Math.Tanh(2) = 0.964027580075817
Math.Tanh(Double.NaN) = NaN
Math.Tanh(Double.NegativeInfinity) = -1
Math.Tanh(Double.PositiveInfinity) = 1

❮ C# Math Methods

5