Java Math hypot()方法及其示例

JAVA朗,数学。hypot() 函数是Java中内置的数学函数,返回欧几里德范数,  sqrt{(x * x + y * y)} .函数返回sqrt(x 2 +y 2. )无中间溢流或底流。

null
  • 如果其中一个参数是无限的,那么结果就是正无穷大。
  • 如果任何一个参数都是NaN,并且两个参数都不是无限的,那么结果就是NaN。

语法:

public static double hypot(double x, double y)
Parameter :
x and y are the values. 

返回: sqrt(x) 2. +y 2. )无中间溢流或底流。

例1: 来展示java的工作原理。朗,数学。hyptot()方法。

// Java program to demonstrate working
// of java.lang.Math.hypot() method
import java.lang.Math;
class Gfg {
// Driver code
public static void main(String args[])
{
double x = 3 ;
double y = 4 ;
// when both are not infinity
double result = Math.hypot(x, y);
System.out.println(result);
double positiveInfinity =
Double.POSITIVE_INFINITY;
double negativeInfinity =
Double.NEGATIVE_INFINITY;
double nan = Double.NaN;
// when 1 or more argument is NAN
result = Math.hypot(nan, y);
System.out.println(result);
// when both arguments are infinity
result = Math.hypot(positiveInfinity,
negativeInfinity);
System.out.println(result);
}
}


输出:

5.0
NaN
Infinity

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享