📜  Java中的 StrictMath hypot() 方法

📅  最后修改于: 2022-05-13 01:55:36.172000             🧑  作者: Mango

Java中的 StrictMath hypot() 方法

根据基本几何,斜边不过是直角三角形的最长边。它是三角形的直角的对边。为了找到直角三角形的斜边的长度,应用了勾股定理。根据这个定理,给定长度为 p 和 b 的三角形的两条垂直边,斜边可以通过以下公式找到$\sqrt{x^2+y^2}$  .
Java.lang.StrictMath.hypot()是 StrictMath 类的内置方法,用于获取给定两侧或参数的斜边或平方和的平方根,即$\sqrt{num1^2+num2^2}$  .该方法排除了所有中间溢出和下溢。它产生了一些特殊的结果:

  • num1num2中的任何一个为无穷大时,该方法返回正无穷大。
  • 当任何一个参数都是 NAN 并且两个参数都不是无限时,它返回 NAN。

句法:

public static double hypot(double num1, double num2)

参数:该方法接受两个Double类型的参数:

  • num1:这是第一个值或任何一侧。
  • num2:这是第二个值或另一边。

返回值:方法返回$\sqrt{num1^2+num2^2}$  即斜边的长度。
例子 :

Input: num1 = 3
       num2 = 4

Output: 5.0

下面的程序说明了Java.lang.StrictMath.hypot() 方法:
方案一:

java
// Java program to illustrate the
// Java.lang.StrictMath.hypot() Method
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double num1 = 11, num2 = 13.8;
 
        // It returns the hypotenuse
        double hypotlen = StrictMath.hypot(num1, num2);
        System.out.println("Length of hypotenuse  of side "
        + num1 + " & " + num2 + " = " + hypotlen);
    }
}


java
// Java program to illustrate the
// Java.lang.StrictMath.hypot() Method
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double num1 = -54, num2 = -24.8;
 
        // It returns the hypotenuse
        double hypotlen = StrictMath.hypot(num1, num2);
        System.out.println("Length of hypotenuse  of side "
        + num1 + " & " + num2 + " = " + hypotlen);
    }
}


java
// Java program to illustrate the
// Java.lang.StrictMath.hypot() Method
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double num1 = 4;
        double positive_Infinity = Double.POSITIVE_INFINITY;
        double negative_Infinity = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
 
        // When 1 or more argument is NAN
        double hypotlen = StrictMath.hypot(nan, num1);
        System.out.println("Hypotenuse length = " + hypotlen);
 
        // When both arguments are infinity
        hypotlen = StrictMath.hypot(positive_Infinity,
                                    negative_Infinity);
        System.out.println("Hypotenuse length = " + hypotlen);
    }
}


输出:
Length of hypotenuse  of side 11.0 & 13.8 = 17.647662734764623

方案二:

Java

// Java program to illustrate the
// Java.lang.StrictMath.hypot() Method
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double num1 = -54, num2 = -24.8;
 
        // It returns the hypotenuse
        double hypotlen = StrictMath.hypot(num1, num2);
        System.out.println("Length of hypotenuse  of side "
        + num1 + " & " + num2 + " = " + hypotlen);
    }
}
输出:
Length of hypotenuse  of side -54.0 & -24.8 = 59.422554640473

方案 3:

Java

// Java program to illustrate the
// Java.lang.StrictMath.hypot() Method
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double num1 = 4;
        double positive_Infinity = Double.POSITIVE_INFINITY;
        double negative_Infinity = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
 
        // When 1 or more argument is NAN
        double hypotlen = StrictMath.hypot(nan, num1);
        System.out.println("Hypotenuse length = " + hypotlen);
 
        // When both arguments are infinity
        hypotlen = StrictMath.hypot(positive_Infinity,
                                    negative_Infinity);
        System.out.println("Hypotenuse length = " + hypotlen);
    }
}
输出:
Hypotenuse length = NaN
Hypotenuse length = Infinity