Skip to content
Related Articles

Related Articles

valarray atan2() function in C++
  • Last Updated : 04 Jan, 2019

The atan2() function is defined in valarray header file. This function calculate inverse tangent of (y/x) value of each element in valarray and returns a valarray containing the inverse tangent of all the elements. where y is the proportion of the y-coordinate and x is the proportion of the x-coordinate.

Syntax:

std::valarray res = atan2 (y-coords, x-coords)

Parameters:The function accepts two mandatory parameters which are X-coords and Y-coords.

Note: If both parameter are valarray objects and their sizes not match, then they behavior as undefined.

Returns: This function returns a valarray containing the inverse tangent of all the elements.



Below programs illustrate the above function:

Example 1:-




// atan2 valarray example
// programs illustrate the atan2() function:
  
#include <iostream>
#include <valarray>
using namespace std;
  
int main()
{
    // intilaize both the array X and Y coords
    double y[] = { 0.0, 3.0, -2.0 };
    double x[] = { -3.0, 3.0, -1.0 };
  
    // intilaize both the valarray X and Y coords
    valarray<double> ycoords(y, 3);
    valarray<double> xcoords(x, 3);
  
    // store results in valarray res
    valarray<double> res = atan2(ycoords, xcoords);
  
    // print results of atan2() function
    cout << "results:";
    for (size_t i = 0; i < res.size(); ++i)
        cout << ' ' << res[i];
    cout << '\n';
  
    return 0;
}

Output:

results: results: 3.14159 0.785398 -2.03444

Example 2:-




// atan2 valarray example
// programs illustrate the atan2() function:
  
#include <iostream>
#include <valarray>
using namespace std;
  
int main()
{
    // intilaize both the array X and Y coords
    double y[] = { 4.0, 5.6, -2.8, 7.3 };
    double x[] = { 5.0, -1.5, 7.0, -0.8 };
  
    // intilaize both the valarray X and Y coords
    valarray<double> ycoords(y, 4);
    valarray<double> xcoords(x, 4);
  
    // store results in valarray res
    valarray<double> res = atan2(ycoords, xcoords);
  
    // print results of atan2() function
    cout << "results:";
    for (size_t i = 0; i < res.size(); ++i)
        cout << ' ' << res[i];
    cout << '\n';
  
    return 0;
}

Output:

results: 0.674741 1.83251 -0.380506 1.67995

Example 3:- Errors and Exceptions: The function returns no matching function for call to error when valarray objects of different sizes are passed as an argument.




// atan2 valarray example
// programs illustrate the atan2() function:
  
#include <iostream>
#include <valarray>
using namespace std;
  
int main()
{
    // intilaize both the array X and Y coords
    double y[] = { -2.8, 7.3 };
    float x[] = { 5.0, -0.8, 3.2, 5, 1 };
  
    // intilaize both the valarray X and Y coords
    valarray<double> ycoords(y, 2);
    valarray<float> xcoords(x, 4);
  
    // store results in valarray res
    valarray<double> res = atan2(ycoords, xcoords);
  
    // print results of atan2() function
    cout << "results:";
    for (size_t i = 0; i < res.size(); ++i)
        cout << ' ' << res[i];
    cout << '\n';
  
    return 0;
}

Output:

prog.cpp: In function 'int main()':
prog.cpp:14:48: error: no matching function for call to 'atan2(std::valarray&, std::valarray&)'
   valarray res = atan2 (ycoords, xcoords);
                                                ^
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :