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:-
CPP
// atan2 valarray example // programs illustrate the atan2() function: #include <iostream> #include <valarray> using namespace std; int main() { // initialize both the array X and Y coords double y[] = { 0.0, 3.0, -2.0 }; double x[] = { -3.0, 3.0, -1.0 }; // initialize 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:-
CPP
// atan2 valarray example // programs illustrate the atan2() function: #include <iostream> #include <valarray> using namespace std; int main() { // initialize 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 }; // initialize 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.
CPP
// atan2 valarray example // programs illustrate the atan2() function: #include <iostream> #include <valarray> using namespace std; int main() { // initialize both the array X and Y coords double y[] = { -2.8, 7.3 }; float x[] = { 5.0, -0.8, 3.2, 5, 1 }; // initialize 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); ^
Please Login to comment...