C library function - atan2()
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
- x -- Floating-point value representing the x-axis coordinate.
- y -- Floating-point value representing the y-axis coordinate.
Return Value
- Returns the arctangent of the point (x, y), in radians. The return value range is [-PI, PI].
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
)
;
}
atan2(y, x)Computes the arctangent of y / x, taking the signs of y and x into account to determine the correct quadrant of the angle.- The result is expressed in radians, with a range of [-PI, PI].
- By multiplying the radian result by
180 / π, it can be converted to degrees.
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:
- function
print_quadrant()accepts the y and x arguments, computes their arctangent, and determines the quadrant of the point based on the result. - The code uses
atan2(y, x)to compute the arctangent and determines the quadrant based on the range of the angle.
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
atan2()It is valid for all real number inputs, so no additional error handling is needed.- The function does not set
errno, nor does it return NaN.
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 -