当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。