Calculating the square root of some number (Java Math sqrt method)
Now let’s move on to our next method, which helps us find the square root of some numbers. The sqrt method takes the double number as an argument and returns its rounded positive square root.
There are some special cases like –
- For an argument less than zero or some invalid input, The output is going to be NaN, which stands for Not A Number.
- If the input is positive infinity, the output is also positive infinity.
- For input like 0, or negative zero, the output is the same as the argument.
In other cases, this method returns the rounded positive square root of the given argument.
Let’s now write a program to understand the above method.
import java.lang.Math;
public class UnderstandingMath {
public static void main(String[] args) {
System.out.println(Math.sqrt(100));
}
}
Executing the above program, we get the output as 10.0, which is desired output. We are going to make use of the above-stated methods as and when required.