tanh() Function

The tanh() function computes the hyperbolic tangent of a given value. It returns a value that represents the ratio of the hyperbolic sine to the hyperbolic cosine of the input.


Syntax of tanh()

</>
Copy
double tanh(double x);
float  tanhf(float x);
long double tanhl(long double x);

Parameters

ParameterDescription
xA value representing a hyperbolic angle.

The function processes the given input to determine its hyperbolic tangent, which is the result of dividing the hyperbolic sine of the input by the hyperbolic cosine of the input.

Return Value

tanh() returns the hyperbolic tangent of the input value.


Examples for tanh()

Example 1: Computing the Hyperbolic Tangent of a Positive Angle

This example demonstrates how to compute the hyperbolic tangent for a positive input value.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double value = 1.0;
    double result = tanh(value);
    printf("tanh(1.0) = %f\n", result);
    return 0;
}

Explanation:

  1. A variable value is initialized with 1.0.
  2. The tanh() function is called to compute the hyperbolic tangent of value.
  3. The result is stored in result and printed using printf().

Program Output:

tanh(1.0) = 0.761594

Example 2: Evaluating tanh() for a Negative Input

This example shows how the tanh() function handles a negative input value.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double value = -1.0;
    double result = tanh(value);
    printf("tanh(-1.0) = %f\n", result);
    return 0;
}

Explanation:

  1. The variable value is set to -1.0.
  2. The tanh() function computes the hyperbolic tangent for this negative value.
  3. The result is output to the console.

Program Output:

tanh(-1.0) = -0.761594

Example 3: Computing tanh() at Zero

This example illustrates the behavior of tanh() when the input is zero.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double value = 0.0;
    double result = tanh(value);
    printf("tanh(0.0) = %f\n", result);
    return 0;
}

Explanation:

  1. The variable value is set to 0.0.
  2. The tanh() function computes the hyperbolic tangent, which is 0 when the input is zero.
  3. The result is printed to the console.

Program Output:

tanh(0.0) = 0.000000

Example 4: Using tanh() with a Large Input Value

This example demonstrates how the tanh() function behaves when provided with a large input value.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double value = 20.0;
    double result = tanh(value);
    printf("tanh(20.0) = %f\n", result);
    return 0;
}

Explanation:

  1. The variable value is assigned a large number, 20.0.
  2. The tanh() function computes the hyperbolic tangent, which approaches 1 for large positive inputs.
  3. The resulting value is then printed.

Program Output:

tanh(20.0) = 1.000000