VOOZH about

URL: https://www.geeksforgeeks.org/cpp/valarray-atan2-function-in-c/

⇱ valarray atan2() function in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

valarray atan2() function in C++

Last Updated : 11 Jul, 2025

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:- 

Output:

results: results: 3.14159 0.785398 -2.03444

Example 2:- 

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. 

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);
 ^
Comment