C library functions -atan2()

C standard library - <math.h>

Description

atan2()is the C standard library<math.h>A function for calculating the arctangent of two parameters. These two parameters 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 usingatan()function is more powerful because it takes into account the signs of x and y, thus determining the correct quadrant.

Declaration

Below is the declaration of the atan2() function.

#include <math.h>

double atan2(double y, double x);
float atan2f(float y, float x);
long double atan2l(long double y, long double x);

Parameter

  • x-- A floating-point value representing the x-axis coordinate.
  • y-- A 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 [-π, π].

Example

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

Example

#include <stdio.h>
#include <math.h>

#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("It is %lf degrees\n", ret);
 
   return(0);
}
  • atan2(y, x)Computes the arctangent of y / x, and considers the signs of y and x to determine the correct quadrant of the angle.
  • The result is expressed in radians, with a range of [-π, π].
  • by multiplying the radian result by180 / π, you can convert it to degrees.

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

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

Determine the quadrant

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

Example

#include <stdio.h>
#include <math.h>

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:

  • Functionprint_quadrant()Accepts y and x parameters, calculates their arctangent value, and determines the quadrant of the point based on the result.
  • Usage in codeatan2(y, x)Computes the arctangent value and determines the quadrant based on the range of the angle.

Let us 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 required.
  • the function does not seterrno, nor will it return NaN.

Summary

atan2()The function is used to compute the arctangent of the point (x, y), making it a powerful tool for angle calculations on a two-dimensional plane. Compared toatan(), it can correctly handle angles in all quadrants and return precise results. In mathematical calculations and graphics processing, the rational use ofatan2()can simplify code and improve accuracy.

C standard library - <math.h>

other extensions