根据基本几何学, 斜边 只不过是直角三角形的最长边。它是与三角形直角相对的边。为了求直角三角形斜边的长度,应用毕达哥拉斯定理。根据这个定理,给定一个长度为p和b的三角形的两条垂直边,斜边可以通过公式求出 . 这个 JAVA朗,数学。hypot() 是StrictMath类的一种内置方法,用于获取给定两边或参数的斜边或平方和的平方根,即。
.该方法排除了所有中间溢流和底流。它产生的特殊结果很少:
null
- 当下列任一情况发生时,该方法返回正无穷大: num1 或 num2 是无限的。
- 当任何一个参数为NAN且两个参数都不是无限时,它返回NAN。
语法:
public static double hypot(double num1, double num2)
参数: 该方法接受两个双重类型的参数:
- num1: 这是第一个值或任意一侧。
- num2: 这是第二个值或另一面。
返回值: 该方法返回 i、 e.斜边的长度。 例如:
Input: num1 = 3 num2 = 4Output: 5.0
下面的程序演示了Java。朗,数学。hypot()方法: 项目1:
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); } } |
输出:
Length of hypotenuse of side 11.0 & 13.8 = 17.647662734764623
项目2:
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 = NaNHypotenuse length = Infinity
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END