Java中的 StrictMath sqrt() 方法
Java.lang.StrictMath.sqrt()是Java中 StrictMath 类的内置方法,用于获取指定 double 值的精确舍入正平方根。
句法:
public static double sqrt(double num)
参数:该函数接受一个 double 类型的参数num ,其平方根将由函数返回。
返回值:此方法返回num的正平方根。它也引起了dout的特殊情况:
- 如果参数为NaN或小于零,则函数返回NaN
- 当参数为正无穷时,该函数返回正无穷。
- 如果参数为零,则函数返回与参数相同符号的零
- 结果是最接近参数值的数学平方根的双精度值。
例子:
Input: num = 25
Output: 5.0
Input: -729
Output: NaN
下面的程序说明了Java.lang.StrictMath.sqrt() 方法的使用:
方案一:
// Java Program to illustrate
// java.lang.StrictMath.sqrt() function
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
double num1 = 289, num2 = -729;
double num3 = 0.0, num4 = 547.87;
// It returns the positive square root
double sqrt_Value = StrictMath.sqrt(num1);
System.out.println("square root = " + sqrt_Value);
sqrt_Value = StrictMath.sqrt(num2);
System.out.println("square root = " + sqrt_Value);
sqrt_Value = StrictMath.sqrt(num3);
System.out.println("square root = " + sqrt_Value);
sqrt_Value = StrictMath.sqrt(num4);
System.out.println("square root = " + sqrt_Value);
}
}
输出:
square root = 17.0
square root = NaN
square root = 0.0
square root = 23.406622994357814
方案二:
// Java Program to illustrate
// java.lang.StrictMath.sqrt() function
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
double num1 = 1160, num2 = -97;
double num3 = -0.0, num4 = 144;
double num5 = -72.18;
// It returns the positive square root
double sqrt_Value = StrictMath.sqrt(num1);
System.out.println("square root = " + sqrt_Value);
sqrt_Value = StrictMath.sqrt(num2);
System.out.println("square root = " + sqrt_Value);
sqrt_Value = StrictMath.sqrt(num3);
System.out.println("square root = " + sqrt_Value);
sqrt_Value = StrictMath.sqrt(num4);
System.out.println("square root = " + sqrt_Value);
sqrt_Value = StrictMath.sqrt(num5);
System.out.println("square root = " + sqrt_Value);
}
}
输出:
square root = 34.058772731852805
square root = NaN
square root = -0.0
square root = 12.0
square root = NaN