C++ tanh()

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

The function is defined in <cmath> header file.

[Mathematics] tanh x = tanh(x) [In C++ Programming]

tanh() prototype [As of C++ 11 standard]

double tanh(double x);
float tanh(float x);
long double tanh(long double x);
double tanh(T x); // For integral type

The tanh() function takes a single argument in radians and returns the hyperbolic tangent of that angle in type double, float or long double type.

The hyperbolic tangent of x is given by,

C++ tanh() function
Formula of tanh()

tanh() Parameters

The tanh() function takes a single mandatory argument representing a hyperbolic angle in radian.


tanh() Return value

The tanh() function returns the hyperbolic tangent of the argument.


Example 1: How tanh() function works in C++?

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double x = 0.50, result;
	result = tanh(x);
	cout << "tanh(x) = " << result << endl;

	// x in Degrees
	double xDegrees = 90;
	x = xDegrees * 3.14159/180;

	result = tanh(x);
	cout << "tanh(x) = " << result << endl;

	return 0;
}

When you run the program, the output will be:

tanh(x) = 0.462117
tanh(x) = 0.917152

Example 2: tanh() function with integral type

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int x = 5;
	double result;

	result = tanh(x);
	cout << "tanh(x) = " << result << endl;
	
	return 0;
}

When you run the program, the output will be:

tanh(x) = 0.999909
Did you find this article helpful?