strictmath
StrictMath類sqrt()方法 (StrictMath Class sqrt() method)
sqrt() Method is available in java.lang package.
sqrt()方法在java.lang包中可用。
sqrt() Method is used to find the square root of the given parameter in the method. Here, "sqrt" stands for square root
- sqrt() Method is a static method so it is accessible with the class name and if we try to access the method with the class object then we will not get any error.
- sqrt() Method does not throw any exception at the time of finding the square root of the given number.
sqrt()方法用于查找方法中給定參數的平方根。 在這里, “ sqrt”代表平方根
- sqrt()方法是靜態方法,因此可以使用類名進行訪問,如果嘗試使用類對象訪問該方法,則不會收到任何錯誤。
- sqrt()方法在查找給定數字的平方根時不會引發任何異常。
Syntax:
句法:
public static double sqrt(double d);
Parameter(s):
參數:
double d – represents for which we have to find the square root.
double d –代表我們必須找到其平方根。
Return value:
返回值:
The return type of the method is double, it returns the square root of the given parameter.
該方法的返回類型為double ,它返回給定參數的平方根。
Note:
注意:
If we pass NaN, the method returns the same (i.e. NaN).
如果我們通過NaN,則該方法返回相同的值(即NaN)。
If we pass zero, the method returns the same value with the same sign.
如果傳遞零,則該方法返回具有相同符號的相同值。
If we pass an argument positive infinity, the method returns the same.
如果我們傳遞參數正無窮大,則該方法將返回相同的結果。
Example:
例:
// Java program to demonstrate the example of sqrt(double d)
// method of StrictMath class.
public class Sqrt {
public static void main(String[] args) {
// variable declarations
double d1 = -0.0;
double d2 = 0.0;
double d3 = -7.0 / 0.0;
double d4 = 7.0 / 0.0;
double d5 = 1000.0;
double d6 = -1000.0;
// Display previous value of d1,d2,d3,d4,d5 and d6
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);
System.out.println("d3: " + d3);
System.out.println("d4: " + d4);
System.out.println("d5: " + d5);
System.out.println("d6: " + d6);
/*Here , we will get (-0.0) because we are passing parameter (-0.0) */
System.out.println("StrictMath.sqrt(d1): " + StrictMath.sqrt(d1));
// Here , we will get (0.0) because we are passing parameter
// (0.0) so the square root is the same
System.out.println("StrictMath.sqrt(d2): " + StrictMath.sqrt(d2));
// Here , we will get (NaN) because we are passing parameter
// (-7.0/0.0) so the square root is (-Infinity)
System.out.println("StrictMath.sqrt(d3): " + StrictMath.sqrt(d3));
// Here , we will get (Infinity) because we are passing
// parameter (7.0/0.0) so the square root is (Infinity)
System.out.println("StrictMath.sqrt(d4): " + StrictMath.sqrt(d4));
// Here , we will get (square root of given argument) because
// we are passing parameter (1000.0)
System.out.println("StrictMath.sqrt(d5): " + StrictMath.sqrt(d5));
// Here , we will get (NaN) because we are passing parameter
// (-1000.0)
System.out.println("StrictMath.sqrt(d6): " + StrictMath.sqrt(d6));
}
}
Output
輸出量
d1: -0.0
d2: 0.0
d3: -Infinity
d4: Infinity
d5: 1000.0
d6: -1000.0
StrictMath.sqrt(d1): -0.0
StrictMath.sqrt(d2): 0.0
StrictMath.sqrt(d3): NaN
StrictMath.sqrt(d4): Infinity
StrictMath.sqrt(d5): 31.622776601683793
StrictMath.sqrt(d6): NaN
翻譯自: https://www.includehelp.com/java/strictmath-sqrt-method-with-example.aspx
strictmath