C++ Valarray::tan() Function



The C++ Valarray::tan() function calculates the tangent of each element's value in a valarray and returns a valarray that contains the tangents of all the elements.

Tangent is a trigonometric function that mathematically describes the angle of a right-angled triangle. It is the ratio of length of the opposite side to the angle and side that is adjacent to the angle. For each element, this function makes a single call to the cmath's tan() function.

Syntax

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

tan(const valarray<Type>& x);

Parameters

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

Examples

Example 1

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

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

int main() {
   valarray<double>
   valarray0 = {1,1.2,3,0.6,4};
   valarray<double> valarray1;
   valarray1 = tan(valarray0);
   
   cout << "The New tan"
      << " 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 New tan Valarray is : 
1.55741 2.57215 -0.142547 0.684137 1.15782   

Example 2

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

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

int main() {
   valarray<double> myvalarray = { 0.3,1.2,2.5,0.6};
   cout << "The Original Valarray is : ";
   
   for (double& ele : myvalarray)
      cout << ele << " ";
   valarray<double> tanValarray;
   tanValarray = tan(myvalarray);
   cout << "\nThe tan Valarray is : ";
   
   for (double& ele : tanValarray)
      cout << ele << " ";
   return 0;
}

Output

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

The Original Valarray is : 0.3 1.2 2.5 0.6 
The tan Valarray is : 0.309336 2.57215 -0.747022 0.684137 

Example 3

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

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

int main() {
   long int a = 4;
   double result;
   result = tan(a);
   cout << "tan(a) = " << result;
   return 0;
}

Output

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

tan(a) = 1.15782

Example 4

Following is the example where we are going to pass degree= '90' to the tan() function and check how it works and retrieve the output.

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

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

Output

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

tan(a) = 0.493467
tan(a) with degree 90 = 753696
Advertisements