java hypot_Java StrictMath hypot()用法及代码示例

根据基本几何形状,斜边仅是right-angled三角形的最长边。它是与三角形直角相反的一侧。为了找到right-angled三角形的斜边的长度,应用勾股定理。根据该定理,给定长度为p和b的三角形的两个垂直边,可以通过以下公式找到斜边

f30fc50cdad2a5f94c2b1241dc36041b.png

的Java.lang.StrictMath.hypot()是StrictMath类的一种内置方法,用于获取给定两侧或自变量的平方和的斜边或square-root。

958b603059f9c9bb4fab43a3ec291172.png。该方法排除了所有中间上溢和下溢。它不会产生一些特殊结果:

当num1或num2为无限时,该方法返回正无穷大。

当任何一个参数为NAN并且两个参数都不为无限时,它将返回NAN。

用法:

public static double hypot(double num1, double num2)

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

num1:这是第一个值或任何一侧。

num2:这是第二个值或另一边。

返回值:方法返回

958b603059f9c9bb4fab43a3ec291172.png即斜边的长度。

例子:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值