Java - StrictMath hypot(double x, double y) 方法



描述

Java StrictMath hypot(double x, double y) 方法返回 sqrt(x2 +y2),避免中間過程出現溢位或下溢。特殊情況:

  • 如果任一引數是無窮大,則結果為正無窮大。

  • 如果任一引數是 NaN 且都不是無窮大,則結果為 NaN。

宣告

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

public static double hypot(double x, double y)

引數

  • x − 一個值

  • y − 一個值

返回值

此方法返回 sqrt(x2 +y2),避免中間過程出現溢位或下溢。

異常

獲取雙精度值的平方根,針對負值示例

以下示例演示了 StrictMath hypot() 方法的用法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two double numbers
      double x = 60984.1;
      double y = -497.99;
   
      // call hypot and print the result
      System.out.println("StrictMath.hypot(" + x + "," + y + ")=" + StrictMath.hypot(x, y));
   }
}

輸出

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

StrictMath.hypot(60984.1,-497.99)=60986.133234122164

獲取雙精度值的平方根,針對負零值示例

以下示例演示了 StrictMath hypot() 方法在零值情況下的用法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two double numbers
      double x = 0.0;
      double y = -0.0;
   
      // call hypot and print the result
      System.out.println("StrictMath.hypot(" + x + "," + y + ")=" + StrictMath.hypot(x, y));
   }
}

輸出

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

StrictMath.hypot(0.0,-0.0)=0.0

獲取雙精度值的平方根,針對負一值示例

以下示例演示了 StrictMath hypot() 方法在 1 值情況下的用法。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get two double numbers
      double x = 1.0;
      double y = -1.0;
   
      // call hypot and print the result
      System.out.println("StrictMath.hypot(" + x + "," + y + ")=" + StrictMath.hypot(x, y));
   }
}

輸出

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

StrictMath.hypot(1.0,-1.0)=1.4142135623730951
java_lang_strictmath.htm
廣告
© . All rights reserved.