Java - StrictMath atan2(double, double) 方法



描述

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

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

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

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

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

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

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

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

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

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

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

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

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

宣告

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

public static double atan2(double y, double x)

引數

  • y − 縱座標

  • x − 橫座標

返回值

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

異常

獲取正值的反正切示例

以下示例演示了使用兩個引數都具有正值的 StrictMath atan2() 方法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

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

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

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

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

輸出

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

StrictMath.atan2(90.0,59.99999999999999)=0.9827937232473292

獲取負值的反正切示例

以下示例演示了使用兩個引數都具有負值的 StrictMath atan2() 方法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

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

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

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

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

輸出

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

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

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

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

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

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

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

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

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

輸出

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

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