# tanh() function in C++ STL

C++ProgrammingServer Side Programming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

The tanh() function returns the hyperbolic tangent of an angle given in radians. It is an inbuilt function in C++ STL.

The syntax of the tanh() function is given as follows.

tanh(var)

As can be seen from the syntax, the function tanh() accepts a parameter var of data type float, double or long double. It returns the hyperbolic tangent of var.

A program that demonstrates tanh() in C++ is given as follows.

## Example

Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double d = 5, ans;
ans = tanh(d);
cout << "tanh("<< d <<") = " << ans << endl;
return 0;
}

## output

tanh(5) = 0.999909

In the above program, first the variable d is initialized. Then hyperbolic tangent of d is found using tanh() and stored in ans. Finally the value of ans is displayed. This is demonstrated by the following code snippet.

double d = 5, ans;
ans = tanh(d);
cout << "tanh("<< d <<") = " << ans << endl;

If the value is provided in degrees, it is converted to radians before using tanh() function.as it returns the hyperbolic tangent of an angle given in radians A program to demonstrate this is as follows −

## Example

Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double degree = 60, ans;
degree = degree * 3.14159/180;
ans = tanh(degree);
cout << "tanh("<<degree<<") = " << ans <<endl;
return 0;
}

## Output

tanh(1.0472) = 0.780714

In the above program, the value is given in degree. So it is converted into radians and then the hyperbolic tangent is obtained using tanh(). Finally, the output is displayed. This is demonstrated by the following code snippet.

double degree = 60, ans;
degree = degree * 3.14159/180;
ans = tanh(degree);
cout << "tanh("<<degree<<") = " << ans <<endl;