C 语言 atan2()

atan2() 函数在 math.h 头文件中定义。它有助于返回给定数字的弧切线值(以弧度为单位)。此函数接受两个参数“x 坐标”和“y 坐标”,并根据两个值的符号确定正确的象限。


double atan2(double y, double x); #where x &y are floating point values

此外,还使用了 atan2f() 和 atan2l() 两个函数,分别对应 float 和 long double 类型。


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

atan2() 参数

atan2() 函数接受两个参数,它们可以是任意数字,正数或负数。

参数 描述 必需/可选
x 表示 x 坐标的浮点值 必需
y 表示 y 坐标的浮点值 必需 

atan2() 返回值

atan2() 函数的返回值是区间 [-pi,+pi] 内的数字,以弧度为单位。

输入 返回值
任何正数或负数 区间 [-pi,+pi] 内的数字

atan2() 示例

示例 1:C 语言中 atan2() 函数的工作原理?


#include <stdio.h>
#include <math.h>
#define PI 3.141592654

int main()
{
  double x, y, output;
  y = 2.53;
  x = -10.2;
  output = atan2(y, x);
  output = output * 180.0/PI;

  printf("The Tangent inverse for(x = %.1lf, y = %.1lf) is %.1lf degrees.", x, y, output);
  return 0;
}

输出


The Tangent inverse for(x = -10.2, y = 2.53) is 166.1 degrees. 

示例 2:C 语言中 atan2() 函数如何工作?


#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int main () {
   double x, y, out, value;

   x = -7.0;
   y = 7.0;
   value = 180.0 / PI;

   out = atan2 (y,x) * value;
   printf("Arc tangent of x = %lf, y = %lf ", x, y);
   printf("is %lf degrees\n", out);
  
   return(0);
}

输出


Arc tangent of x = -7.000000, y = 7.000000 is 135.000000 degrees