A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_atan2.htm below:

C++ atan2 Function

C++ Valarray::atan2 Function

The C++ Valarray::atan2() function calculates the inverse tangent of the (y/x) value of each element in the valarray and returns a valarray containing the inverse tangent of all the items. where y is the proportion of the y-coordinate and x is the proportion of the x-coordinate.

The atan2() function of cmath is overload with in this function, which calls it once for each element. It uses the sings of the arguments to determine the appropriate quadrant.

Syntax

Following is the syntax for C++ Valarray::atan2 Function −

atan2 (const valarray<T>& y, const valarray<T>& x);
atan2 (const valarray<T>& y, const T& x);
atan2 (const T& y, const valarray<T>& x);
Parameters Examples Example 1

Let's look into the following example, where we are going to use atan2() function and retrieving the output.

#include <iostream>
#include <valarray>
using namespace std;

int main() {
   double y[] = { 0.4, 1.2, -1.0 };
   double x[] = { 0.5, 1.3, -1.0 };
   valarray<double> ycoords(y, 2);
   valarray<double> xcoords(x, 2);
   valarray<double> result = atan2(ycoords, xcoords);
   cout << "Result:";
   
   for (size_t i = 0; i < result.size(); ++i)
      cout << ' ' << result[i];
   cout << '\n';
   return 0;
}
Output

Let us compile and run the above program, this will produce the following result −

Result: 0.674741 0.745419
Example 2

Considering the another scenario, where we are going to use the atan2() function and retrieving the output.

#include <iostream>
#include <valarray>
using namespace std;

int main (){
   valarray<double> y = {4, 10, 9};
   valarray<double> x = {12, 22, 14};
   valarray<double> result1 = atan2(y, x);
   cout<<"\natan2(y, x) returns: ";
   
   for(int i = 0; i < result1.size(); i++)
      cout<<result1[i]<<" ";  
   return 0;
}
Output

Let us compile and run the above program, this will produce the following result −

atan2(y, x) Return: 0.321751 0.426627 0.571337  
Example 3

In the following example, we are going to use atan2() function and retrieving the output in radians.

#include <iostream>
#include <cmath>
#define PI 3.141592654
using namespace std;

int main() {
   double result;
   int x = -12;
   float y = 31.6;
   result = atan2(y, x);
   cout << "atan2(y/x) = " << result << " radians" << endl;
   return 0;
}
Output

Let us compile and run the above program, this will produce the following result −

atan2(y/x) = 1.93372 radians
Example 4

Following is the example, where we are going to use atan2() function and retrieving the output in degrees.

#include <iostream>
#include <cmath>
#define PI 3.141592654
using namespace std;

int main() {
   double result;
   int x = -12;
   float y = 31.6;
   result = atan2(y, x);
   cout << "atan2(y/x) = " << result * (180 / PI) << " degrees";
   return 0;
}
Output

Let us compile and run the above program, this will produce the following result −

atan2(y/x) = 110.794 degrees

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4