當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Math.hypot()用法及代碼示例

java.lang.Math.hypot() 用於返回指定參數的平方和的平方根,沒有中間溢出或下溢。

用法

public static double hypot(double x, double y)

參數

x = a value
y = a value

返回

它返回 sqrt(x2+ y2) 沒有中間溢出或下溢。
  • 如果參數為正值或負值,則此方法將返回輸出。
  • 如果參數是正無窮大或負無窮大,則此方法將返回正無窮大。
  • 如果參數是 NaN 並且兩個參數都不是無限的,則此方法將返回 NaN。

例子1

public class HypotExample1
{
    public static void main(String[] args) 
    {
        double a = 8;
        double b = 6;
        // Return the value of sqrt(a power of 2 + b power of 2)
        System.out.println(Math.hypot(a, b));
    }
}

輸出:

10.0

例子2

public class HypotExample2
{
    public static void main(String[] args) 
    {
        double x = -4;
        double y = 3;
        // Return the value of sqrt((-4) power of 2 + (3) power of 2)
        System.out.println(Math.hypot(x, y));
    }
}

輸出:

5.0

例子3

public class HypotExample3
{
    public static void main(String[] args) 
    {
        double a = Double.POSITIVE_INFINITY;
        double b = 73;
        // when 1 or more argument is Infinity, output Infinity
        System.out.println(Math.hypot(a, b));
    }
}

輸出:

Infinity

示例 4

public class HypotExample4
{
    public static void main(String[] args) 
    {
        double a = 0.0/0;
        double b = 67;
        // when 1 or more argument is NaN, output NaN
        System.out.println(Math.hypot(a, b));
    }
}

輸出:

NaN






相關用法


注:本文由純淨天空篩選整理自 Java Math.hypot() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。