Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - atan2()

C library function - atan2()

C Standard Library - <math.h> C Standard Library -

Description

atan2() is a function in the C standard library is a function used to compute the arctangent of two arguments. The two arguments represent the y-coordinate and x-coordinate of a point,atan2() The function returns the angle from the x-axis to the point (x, y), in radians. This function is more powerful than using atan() alone, because it takes the signs of x and y into account and can therefore determine the correct quadrant.

Declaration

The following is the declaration of the atan2() function.



#include 



double atan2(double y, double x);

float atan2f(float y, float x);

long double atan2l(long double y, long double x);

Parameters

Return Value

Example

The following example demonstrates the use of the atan2() function.

Example

#include

#include

#define PI 3.14159265

int

main

(

)

{

   

double

x

,

y

,

ret

,

val

;

   x

=

7.0

;

   y

=

7.0

;

   val

=

180.0

/

PI

;

   ret

=

atan2

(

y

,

x

)

val

;

   

printf

(

The arctangent of x = %lf, y = %lf

,

x

,

y

)

;

   

printf

(

is %lf degrees\n

,

ret

)

;

 

   

return

(

0

)

;

}

Let's compile and run the above program, which will produce the following result:



x = -7.000000, y = 7.000000 的反正切是 135.000000 度

Determining the quadrant

The following example shows how to use atan2() to determine the quadrant of a point:

Example

#include

#include

void

print_quadrant

(

double

y

,

double

x

)

{

   

double

angle

=

atan2

(

y

,

x

)

;

   

printf

(

"Point (%f, %f) is in quadrant: "

,

x

,

y

)

;

   

if

(

angle

0

&&

angle

<

M_PI

/

2

)

{

printf

(

"I\n"

)

;

   

}

else

if

(

angle

M_PI

/

2

&&

angle

<

M_PI

)

{

printf

(

"II\n"

)

;

   

}

else

if

(

angle

<

0

&&

angle

M_PI

/

2

)

{

printf

(

"IV\n"

)

;

   

}

else

{

       

printf

(

"III\n"

)

;

   

}

}

int

main

(

)

{

    print_quadrant

(

1.0

,

1.0

)

;

 

// I

    print_quadrant

(

1.0

,

1.0

)

;

// II

    print_quadrant

(

1.0

,

1.0

)

;

// III

    print_quadrant

(

1.0

,

1.0

)

;

// IV

   

return

0

;

}

Code analysis:

Let's compile and run the above program, which will produce the following result:



Point (1.000000, 1.000000) is in quadrant: I

Point (-1.000000, 1.000000) is in quadrant: II

Point (-1.000000, -1.000000) is in quadrant: III

Point (1.000000, -1.000000) is in quadrant: IV

Error handling

Summary

atan2() The function computes the arctangent of the point (x, y) and is a powerful tool for angle calculations on a two-dimensional plane. Compared with atan(), it correctly handles angles in all quadrants and returns precise results. In mathematical calculations and graphics processing, appropriate use of atan2() can simplify code and improve accuracy.

C Standard Library - <math.h> C Standard Library -