atan2()

The atan2() function returns the inverse tangent of the quotient of a coordinate pair (y/x), in radians.

Syntax

atan2(y, x)

The data type of the y and x parameters can be double, float, or long double.

  • The return value has the range of [-π, π].
  • A negative value is returned if the y parameter is negative.
  • If both parameters are 0, then the value is 0.

Example

The following is an example of the atan2() function and features an edge case:

#include <iostream>
#include <cmath>
int main() {
double x = 10.0, y = 20.0;
std::cout << atan2(y, x) << std::endl;
x = 0.0, y = 0.0; // Edge Case
std::cout << atan2(y, x) << std::endl;
return 0;
}

This will produce the following output:

1.10715
0

Codebyte Example

The following example is runnable and features a negative y-coordinate:

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C++ on Codecademy