Home » C++ programming language

atan2() function with example in C++

C++ atan2() function: Here, we are going to learn about the atan2() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on April 28, 2019

C++ atan2() function

atan2() function is a library function of cmath header, it is used to find the principal value of the arc tangent of y/x, where y is the proportion of the y-coordinate and x is the proportion of the x-coordinate , it accepts two arguments (y, x) and returns arc tangent of y/x in radians.

Syntax of atan2() function:

    atan2(y, x);

Parameter(s): y, x – are the numbers (proportions of y-coordinate and x-coordinate) to calculate the principal value of the arc tangent of y/x.

Return value: double – it returns double type value that is the principal value of the arc tangent of y/x.

Example:

    Input:
    float x = -1.0;
    float y = -2.5;
    
    Function call:
    atan2(y, x);    
    
    Output:
    -1.9513

C++ code to demonstrate the example of atan2() function

// C++ code to demonstrate the example of 
// atan2() function

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

// main() section
int main()
{
    float x;
    float y;
    
    x = -1.0;
    y = -2.5;
    cout<<"atan2("<<y<<","<<x<<"): "<<atan2(y,x)<<endl;

    x = 11.0;
    y = 22.5;
    cout<<"atan2("<<y<<","<<x<<"): "<<atan2(y,x)<<endl;

    x = -1.0;
    y = 0;
    cout<<"atan2("<<y<<","<<x<<"): "<<atan2(y,x)<<endl;    
    
    return 0;
}

Output

atan2(-2.5,-1): -1.9513
atan2(22.5,11): 1.11608
atan2(0,-1): 3.14159

Reference: C++ atan2() function




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.