Java - Math atan2(double, double) 方法



描述

Java Math atan2(double y,double x) 方法將直角座標 (x, y) 轉換為極座標 (r, theta)。此方法透過計算 -pi 到 pi 範圍內的 y/x 的反正切來計算相位 theta。特殊情況:

  • 如果任一引數為 NaN,則結果為 NaN。

  • 如果第一個引數為正零且第二個引數為正,或者第一個引數為正且有限且第二個引數為正無窮大,則結果為正零。

  • 如果第一個引數為負零且第二個引數為正,或者第一個引數為負且有限且第二個引數為正無窮大,則結果為負零。

  • 如果第一個引數為正零且第二個引數為負,或者第一個引數為正且有限且第二個引數為負無窮大,則結果為最接近 pi 的雙精度值。

  • 如果第一個引數為負零且第二個引數為負,或者第一個引數為負且有限且第二個引數為負無窮大,則結果為最接近 -pi 的雙精度值。

  • 如果第一個引數為正且第二個引數為正零或負零,或者第一個引數為正無窮大且第二個引數為有限值,則結果為最接近 pi/2 的雙精度值。

  • 如果第一個引數為負且第二個引數為正零或負零,或者第一個引數為負無窮大且第二個引數為有限值,則結果為最接近 -pi/2 的雙精度值。

  • 如果兩個引數均為正無窮大,則結果為最接近 pi/4 的雙精度值。

  • 如果第一個引數為正無窮大且第二個引數為負無窮大,則結果為最接近 3*pi/4 的雙精度值。

  • 如果第一個引數為負無窮大且第二個引數為正無窮大,則結果為最接近 -pi/4 的雙精度值。

  • 如果兩個引數均為負無窮大,則結果為最接近 -3*pi/4 的雙精度值。

結果必須在正確舍入結果的 2 ulp 以內。結果必須是半單調的。

宣告

以下是java.lang.Math.atan2() 方法的宣告

public static double atan2(double y, double x)

引數

  • y − 縱座標

  • x − 橫座標

返回值

此方法返回對應於笛卡爾座標系中點 (x, y) 的極座標系中點 (r, theta)theta 分量。

異常

獲取正值的反正切示例

以下示例顯示了使用兩個引數均為正值的 Math atan2() 方法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a variable x which is equal to PI/2
      double x = Math.PI / 2;

      // get a variable y which is equal to PI/3
      double y = Math.PI / 3;

      // convert x and y to degrees
      x = Math.toDegrees(x);
      y = Math.toDegrees(y);

      // get the polar coordinates
      System.out.println("Math.atan2(" + x + "," + y + ")=" + Math.atan2(x, y));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.atan2(90.0,59.99999999999999)=0.9827937232473292

獲取負值的反正切示例

以下示例顯示了使用兩個引數均為負值的 Math atan2() 方法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a variable x which is equal to -PI/2
      double x = -(Math.PI / 2);

      // get a variable y which is equal to -PI/3
      double y = -(Math.PI / 3);

      // convert x and y to degrees
      x = Math.toDegrees(x);
      y = Math.toDegrees(y);

      // get the polar coordinates
      System.out.println("Math.atan2(" + x + "," + y + ")=" + Math.atan2(x, y));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.atan2(-90.0,-59.99999999999999)=-2.158798930342464

獲取第一個引數為正,第二個引數為負的反正切示例

以下示例顯示了使用第一個引數為正,第二個引數為負值的 Math atan2() 方法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a variable x which is equal to PI/2
      double x = Math.PI / 2;

      // get a variable y which is equal to -PI/3
      double y = -(Math.PI / 3);

      // convert x and y to degrees
      x = Math.toDegrees(x);
      y = Math.toDegrees(y);

      // get the polar coordinates
      System.out.println("Math.atan2(" + x + "," + y + ")=" + Math.atan2(x, y));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.atan2(90.0,-59.99999999999999)=2.158798930342464
java_lang_math.htm
廣告
© . All rights reserved.