valarray atan2() function in C++
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);
^
Recommended Posts:
- atan2() function in C++ STL
- valarray abs() function in C++
- valarray exp() function in C++
- valarray sin() function in C++
- valarray log() function in C++
- valarray tan() function in C++
- valarray pow() function in C++
- valarray cos() function in C++
- valarray end() function in C++
- max() function for valarray in C++
- valarray size() function in C++
- valarray swap() function in c++
- valarray sqrt() function in C++
- valarray begin() function in C++
- cshift() function for valarray in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



