C++ Standard Library C++ STL Library

C++ <valarray> - tan() Function



The C++ <valarray> tan() function returns a valarray containing trigonometric tangent value of all the elements of valarray va, in the same order. This function overloads with cmath's tan() function and calls this function once for each element.

Syntax

template<class T> valarray<T> tan (const valarray<T>& va);

Parameters

va Specify valarray containing elements of a type for which the function tan() is defined.

Return Value

Returns valarray containing trigonometric tangent value of all the elements of va.

Example:

The example below shows the usage of tan() function.

#include <iostream>
#include <valarray>
using namespace std;
 
const double pi = acos(-1);

int main (){
  valarray<double> va1 = {0, pi/3, pi/4, -pi/4};
  
  //va2 will contain trigonometric tangent value
  //of all elements of va1
  valarray<double> va2 = tan(va1);

  cout<<fixed;
  cout<<"va1 contains: ";
  for(int i = 0; i < va1.size(); i++)
    cout<<va1[i]<<" ";

  cout<<"\nva2 contains: ";
  for(int i = 0; i < va2.size(); i++)
    cout<<va2[i]<<" ";   
  
  return 0;
}

The output of the above code will be:

va1 contains: 0.000000 1.047198 0.785398 -0.785398 
va2 contains: 0.000000 1.732051 1.000000 -1.000000 

❮ C++ <valarray> Library