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



描述

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

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

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

宣告

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

public static double hypot(double x, double y)

引數

  • x − 一個值

  • y − 一個值

返回值

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

異常

獲取負值的double值的平方根示例

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

package com.tutorialspoint;

public class MathDemo {

   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("Math.hypot(" + x + "," + y + ")=" + Math.hypot(x, y));
   }
}

輸出

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

Math.hypot(60984.1,-497.99)=60986.133234122164

獲取負零值的double值的平方根示例

以下示例演示了 Math hypot() 方法在零值上的用法。

package com.tutorialspoint;

public class MathDemo {

   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("Math.hypot(" + x + "," + y + ")=" + Math.hypot(x, y));
   }
}

輸出

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

Math.hypot(0.0,-0.0)=0.0

獲取負一值的double值的平方根示例

以下示例演示了 Math hypot() 方法在1值上的用法。

package com.tutorialspoint;

public class MathDemo {

   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("Math.hypot(" + x + "," + y + ")=" + Math.hypot(x, y));
   }
}

輸出

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

Math.hypot(1.0,-1.0)=1.4142135623730951
java_lang_math.htm
廣告