C++ Valarray::atan Function



The atan() function in the valarray class is used to create a valarray with elements calculated as tan-1 for each element in the original valarray.

The atan() function of cmath is overload with in this function, which calls it once for each element. Atan() returns values in the range [-x/2,x/2] and is compatible with all real numbers.

Syntax

Following is the syntax for C++ Valarray::atan Function −

atan (const valarray<T>& x);

Parameters

x − It is containing elements of a type for which the unary function atan is defined.

Examples

Example 1

Let's look into the following example, where we are going to take number as a input, passing it to the atan() function and retrieving the output.

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

int main() {
   double x =12.43,result;
   result =atan (x);
   cout << "atan(""<<x<<"") = " << result << endl;
   return 0;
}

Output

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

atan(<x<) = 1.49052

Example 2

Following is the example, where we are going to use atan() function and retrieving the output of both original valarray and atan valarray.

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

int main() {
   valarray<double> myvalarray = { 1.2, -0.6, 4, -5.67, -2 };
   cout << "The Orignal Valarray : ";
   
   for (double& ele : myvalarray)
      cout << ele << " ";
   valarray<double> atanvalarray = atan(myvalarray);
   cout << "\nThe atan Valarray : ";
   
   for (double& ele : atanvalarray)
      cout << ele << " ";
   return 0;
}

Output

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

The Orignal Valarray : 1.2 -0.6 4 -5.67 -2 
The atan Valarray : 0.876058 -0.54042 1.32582 -1.39622 -1.10715  

Example 3

Considering the another scenario, where we are going to use atan() function and retrieving the output in degrees.

#include <iostream>
#include <cmath>
#define PI 3.141592654
using namespace std;

int main() {
   int a = 25;
   double result;
   result = atan(a);
   cout << "atan(a) = " << result*180/3.1415 << " degrees" << endl;
   return 0;
}

Output

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

atan(a) = 87.712 degrees

Example 4

In the following example, we are going to use the atan() function and retrieving the output in radians.

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

int main() {
   double x = 142.5;
   double resultRadians = atan(x);
   cout << "atan(x) = " << resultRadians << " radians" << endl;
   return 0;
}

Output

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

atan(x) = 1.56378 radians
Advertisements