![]() |
VOOZH | about |
The Math.sqrt() method is a part of java.lang.Math package. This method is used to calculate the square root of a number. This method returns the square root of a given value of type double. In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.
Special Cases:
These special cases make sure that the Math.sqrt() methods work correctly.
public static double sqrt(double a)
Now, we are going to discuss some examples for better understanding and clarity.
Example 1: In this example, we will see the square root of positive numbers.
5.477225575051661 6.708203932499369 7.745966692414834 9.486832980505138
Explanation: Here, we are calculating the square root of different numbers like 30, 45, 60 and 90.
Example 2: In this example, we will see how the sqrt() method handles special cases like NaN, negative values and posititve infinity.
NaN Infinity NaN
Explanation: Here, we are handling special cases such as NaN and Infinity. When the given argument is negative and NaN then we will get NaN as an output, and when the argument is positive infinity then we will get infinity.