根据基本几何形状,斜边仅是right-angled三角形的最长边。它是与三角形直角相反的一侧。为了找到right-angled三角形的斜边的长度,应用勾股定理。根据该定理,给定长度为p和b的三角形的两个垂直边,可以通过以下公式找到斜边
。
的Java.lang.StrictMath.hypot()是StrictMath类的一种内置方法,用于获取给定两侧或自变量的平方和的斜边或square-root。
。该方法排除了所有中间上溢和下溢。它不会产生一些特殊结果:
当num1或num2为无限时,该方法返回正无穷大。
当任何一个参数为NAN并且两个参数都不为无限时,它将返回NAN。
用法:
public static double hypot(double num1, double num2)
参数:该方法接受Double类型的两个参数:
num1:这是第一个值或任何一侧。
num2:这是第二个值或另一边。
返回值:方法返回
即斜边的长度。
例子:
Input: num1 = 3
num2 = 4
Output: 5.0
以下示例程序旨在说明Java.lang.StrictMath.hypot()方法:
示例1:
// Java praogram 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);
}
}
输出:
Length of hypotenuse of side 11.0 & 13.8 = 17.647662734764623
示例2:
// Java praogram 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 praogram 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