C 庫 - atan2() 函式



C 的數學atan2() 函式用於根據兩個值的符號確定正確的象限,返回y/x 的反正切值(以弧度為單位)。

反正切,也稱為反正切函式。它是正切函式的反函式,它反轉正切函式的效果。

此方法返回的角度是從正 x 軸到該點逆時針測量。

語法

以下是 C atan2() 函式的語法 -

double atan2( double y, double x );

引數

此函式接受以下引數 -

  • x - 它表示浮點型別的 x 座標。

  • y - 它表示浮點型別的 y 座標。

返回值

此函式返回將直角座標 (𝑥, 𝑦) 轉換為極座標 (𝑟, Θ) 後得到的角度 Θ(以弧度為單位)。

示例 1

以下是一個基本的 C 程式,用於演示使用 atan2() 獲取以弧度表示的角度。

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

int main() {
   double x = 1.0;
   double y = 1.0;
   double theta = atan2(y, x);
   printf("The angle is %f radians.\n", theta);
   return 0;
}

輸出

以下是輸出 -

The angle is 0.785398 radians.

示例 2

讓我們再舉一個例子,點 (x,y) = (−1,1) 在第二象限。 atan2() 函式計算與正 x 軸形成的角度。

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

int main() {
   double x = -1.0;
   double y = 1.0;
   double theta = atan2(y, x);
   printf("The angle is %f radians.\n", theta);
   return 0;
}

輸出

以下是輸出 -

The angle is 2.356194 radians.

示例 3

現在,建立另一個 C 程式以顯示反正切值(以度為單位)。

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

int main () {
   double x, y, ang_res, val;

   x = 10.0;
   y = 10.0;
   val = 180.0 / PI;

   ang_res = atan2 (y,x) * val;
   printf("The arc tangent of x = %lf, y = %lf ", x, y);
   printf("is %lf degrees\n", ang_res);
  
   return(0);
}

輸出

以下是輸出 -

The arc tangent of x = 10.000000, y = 10.000000 is 45.000000 degrees
廣告