Math Tanh Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Tanh Function

Math Tanh Function

BLUF: Mastering Math Tanh Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Tanh Function

C++ is renowned for its efficiency. Learn how Math Tanh Function enables low-level control and high-performance computing in the tutorial below.

C++ Math tanh

The function calculates the hyperbolic tangent of an angle provided in radians.

Mathematically,

Syntax

Suppose the angle is 'x':

Example

float tanh(float x);
double tanh(double x);
long double tanh(long double x);
double tanh(integral x);

Parameter

x : The value for which the hyperbolic tangent needs to be calculated.

Return value

It returns the hyperbolic tangent of x.

Example 1

Let's consider a straightforward scenario where the variable x is of type integer.

Example

#include <iostream>
#include<math.h>
using namespace std;
 int main()
{
    int degree =60;
    float x = degree*3.14/180;
    std::cout << "Value of degree is : " <<degree<< std::endl;
    cout<<"tanh(x) : "<<tanh(x);
    return 0;
}

Output:

Output

Value of degree is : 60
tanh(x) : 0.780507

In this instance, the tanh(x) function calculates the hyperbolic tangent of x and yields a result of 0.78.

Example 2

Let's explore a basic scenario where the variable x holds a floating-point data type.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    float degree = 45.2;
    float x = degree*3.14/180;
    std::cout << "Value of degree is : " <<degree<< std::endl;
    cout<<"tanh(x) : "<<tanh(x);
    return 0;
}

Output:

Output

Value of degree is : 45.2
tanh(x) : 0.657552

In this instance, the tanh(x) function calculates the hyperbolic tangent of x, resulting in a value of 0.65.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience