C++ Valarray::tanh() Function



This function generates the hyperbolic tangent of the value of each element in valarray and returns a valarray containing the hyperbolic tangent of all the elements.

The ratio of the hyperbolic sine and cosine functions is known as the hyperbolic tangent function. For each element, this function makes a single call to cmath's tanh() function. It works with the input valarray's elements.

Syntax

Following is the syntax for C++ Valarray::tanh() Function −

tanh(const valarray<Type>& x);

Parameters

x − It contains elements of a type for which the unary function tanh is defined.

Examples

Example 1

Let's consider the following example, where we are going to use tanh() function and retrieving output.

#include <iostream>
#include <valarray>
using namespace std;

int main() { 
   valarray<double>
   valarray0 = {1,0.2,0,1.3,2};
   valarray<double> valarray1;
   valarray1 = tanh(valarray0);
   cout << "The tanh"
      << " Valarray is : "
      << endl;
   for (double& x : valarray1) {
      cout << x << " ";
   }
   cout << endl;
   return 0;
}

Output

When we compile and run the above program, this will produce the following result −

The tanh Valarray is : 
0.761594 0.197375 0 0.861723 0.964028   

Example 2

Consider the following example, where we are going to use tanh() function and getting output of both original valarray and tanh valarray.

#include <iostream>
#include <valarray>
using namespace std;

int main() {
   valarray<double> myvalarray = {1,0.2,0.5,-2.3};
   cout << "The Original Valarray is : ";
   
   for (double& ele : myvalarray)
      cout << ele << " ";
   valarray<double> tanhvalarray = tanh(myvalarray);
   cout << "\nThe tanh Valarray is : ";
   
   for (double& ele : tanhvalarray)
      cout << ele << " ";
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

The Original Valarray is : 1 0.2 0.5 -2.3 
The tanh Valarray is : 0.761594 0.197375 0.462117 -0.980096

Example 3

In the following example, we are going to use the integral type with the tanh() function and retrieving the output.

#include <iostream>
#include <valarray>
using namespace std;

int main() {
   int a = 2;
   double Result;
   Result = tanh(a);
   cout << "Result : " << Result << endl;
   return 0;
}

Output

On running the above program, it will produce the following result −

Result : 0.964028

Example 4

Following is the example where we are going to pass degree= '30' to the tanh() function and checking how it works and retrieving the output.

#include <iostream>
#include <valarray>
using namespace std;

int main() {
   double a = 1.2, result;
   result = tanh(a);
   cout << "tanh(a) = " << result << endl;
   double xDegree = 30;
   a = xDegree * 3.14159/180;
   result = tanh(a);
   cout << "tanh(a) with degree 30 = " << result << endl;
   return 0;
}

Output

When we compile and run the above program, this will produce the following result −

tanh(a) = 0.833655
tanh(a) with degree 30 = 0.480472
Advertisements