Math Atan2 Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Atan2 Function

Math Atan2 Function

BLUF: Mastering Math Atan2 Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Atan2 Function

C++ is renowned for its efficiency. Learn how Math Atan2 Function enables low-level control and high-performance computing in the tutorial below.

C++ Math atan2

The function calculates the arctangent of a set of coordinates.

Suppose the coordinate is (x,y) :

Syntax

Suppose the coordinate is (x,y). Syntax would be:

Example

float atan2(float y, float x);
double atan2(double y, double x);
long double atan2(long double y, long double x);
Promoted atan2(Arithmetic1 y, Arithmetic x );

Parameter

y : It represents the y-coordinate value.

x : It represents the x-coordinate value.

Return value

It provides the result within the range[-?, ?]. If both x and y have a value of zero, the function will return zero.

  • In case any parameter is of integer data type, it will be converted to double.
  • When any argument is of type long double, it will be converted to long double.
  • Example 1

Let's consider a basic scenario where both variables x and y are equal to zero.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    int x=0;
    int y=0;
    cout<<"Value of tan(y/x) is : "<<tan(y/x)<<'\n';
    std::cout << "Value of tan-1(y/x) is : " <<atan2(y,x)<< std::endl;
    return 0;
}

Output:

Output

Value of tan(y/x) is : 0
Value of tan-1(y/x) is : 0

In this instance, the atan2 function computes the arctangent when 'x' and 'y' are both zero.

Example 2

Let's examine a straightforward scenario where both 'x' and 'y' have distinct data types.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    int x=6;
    float y=7.8;
    cout<<"Value of tan(y/x) is : "<<tan(y/x)<<'\n';
    std::cout << "Value of tan-1(y/x) is : " <<atan2(y,x)<< std::endl;
    return 0;
}

Output:

Output

Value of tan(y/x) is : 3.6021
Value of tan1(y/x) is : 0.915101

In this instance, the atan2 method calculates the arctangent based on an integer value for x and a floating-point value for y.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience