C atan2() 原型
double atan2(double y, double x);
atan2() 函数接收两个参数:x 坐标和 y 坐标,并计算象限的角度(以弧度为单位)。
为了更好地理解 atan2()
[Mathematics] tan-1(y/x) = atan2(y,x) [In C programming]
C 语言中还有另外两个函数 atan2f() 和 atan2l(),它们分别专门用于处理 float
和 long double
类型。
atan2()
函数定义在 <math.h> 头文件中。
C atan2() 范围
atan2() 的参数可以是任何数字,无论是正数还是负数。
示例:C atan2() 函数
#include <stdio.h>
#include <math.h>
#define PI 3.141592654
int main()
{
double x, y, result;
y = 2.53;
x = -10.2;
result = atan2(y, x);
result = result * 180.0/PI;
printf("Tangent inverse for(x = %.1lf, y = %.1lf) is %.1lf degrees.", x, y, result);
return 0;
}
输出
Tangent inverse for(x = -10.2, y = 2.53) is 166.1 degrees.
使用 atan2() 时需要注意
传递的第二个参数的值不应为 0。如果传递的第二个参数为 0,程序将无法正常运行。