C++ Standard Library C++ STL Library

C++ <valarray> - atan2() Function



The C++ <valarray> atan2() function returns a valarray containing inverse tangent of y/x using the signs of arguments to determine the appropriate quadrant.

  • If y and x both are valarrays of same size, the function computes the inverse tangent of each pair of corresponding values from y and x. The behavior is undefined if size of y and x are not equal.
  • If y is a valarray and x is a value, the function computes the inverse tangent of each element in y paired with x.
  • If y is a value and x is a valarray, the function computes the inverse tangent of y paired with each element in x.

This function overloads with cmath's atan2() function.

Syntax

template<class T> valarray<T> atan2 
  (const valarray<T>& y, const valarray<T>& x);
template<class T> valarray<T> atan2 
  (const valarray<T>& y, const T& x);
template<class T> valarray<T> atan2 
  (const T& y, const valarray<T>& x);

Parameters

y Specify a valarray or a value with the y coordinate(s).
x Specify a valarray or a value with the x coordinate(s).

Return Value

Returns valarray containing the results of computation of inverse tangent.

Example:

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

#include <iostream>
#include <valarray>
using namespace std;
 
int main (){
  valarray<double> va1 = {1, 2, 3};
  valarray<double> va2 = {3, 2, 1};

  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]<<" ";

  valarray<double> result1 = atan2(va1, va2);
  cout<<"\natan2(va1, va2) returns: ";
  for(int i = 0; i < result1.size(); i++)
    cout<<result1[i]<<" ";  

  valarray<double> result2 = atan2(va1, 2.0);
  cout<<"\natan2(va1, 2.0) returns: ";
  for(int i = 0; i < result2.size(); i++)
    cout<<result2[i]<<" ";  

  valarray<double> result3 = atan2(2.0, va1);
  cout<<"\natan2(2.0, va2) returns: ";
  for(int i = 0; i < result3.size(); i++)
    cout<<result3[i]<<" ";  

  return 0;
}

The output of the above code will be:

va1 contains: 1 2 3 
va2 contains: 3 2 1 
atan2(va1, va2) returns: 0.321751 0.785398 1.24905 
atan2(va1, 2.0) returns: 0.463648 0.785398 0.982794 
atan2(2.0, va2) returns: 1.10715 0.785398 0.588003 

❮ C++ <valarray> Library