Java StrictMath hypot()方法

来源:这里教程网 时间:2026-02-17 21:13:15 作者:

Java StrictMath hypot()方法

根据基本的几何学原理, 斜边 就是直角三角形的最长边。它是与三角形的直角相对的那条边。为了找到直角三角形斜边的长度,可以应用勾股定理。根据该定理,给定长度为p和b的三角形的两条垂直边,斜边可以通过公式

Java.lang.StrictMath.hypot() 是StrictMath类的一个内置方法,用于获取斜边或给定两边或参数的平方根之和,即。该方法排除了所有中间的溢出和下溢。它产生了一些特殊的结果:

当num1或num2为无限时,该方法返回正无穷。当任何一个参数是NAN,并且两个参数都不是无限时,它返回NAN。

语法

public static double hypot( _double num1, double num2_ )

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

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

返回值: 该方法返回斜边的长度。

示例

输入: num1 = 3
num2 = 4

输出:5.0

以下程序说明了Java.lang.StrictMath.hypot()方法:

程序1

// Java program to illustrate the// Java.lang.StrictMath.hypot() Methodimport 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 program to illustrate the// Java.lang.StrictMath.hypot() Methodimport 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 program to illustrate the// Java.lang.StrictMath.hypot() Methodimport 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 = NaNHypotenuse length = Infinity

相关推荐