Open In App

StrictMath sqrt() Method in Java

Last Updated : 09 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The java.lang.StrictMath.sqrt() is an inbuilt method of StrictMath class in Java which is used to obtain the exact rounded positive square root of a specified double value. Syntax:

public static double sqrt(double num)

Parameters: The function accepts a single parameter num of double type, whose square root is to be returned by the function. Return Values: This method returns the positive square root of num. it also gives rise to doubt special cases:

  • The function returns NaN if the argument is NaN or less than zero
  • The function returns positive infinity when the argument is positive infinite.
  • The function returns zero with the same sign as the argument if the argument is zero
  • The result is the double value nearest to the mathematical square root of the argument value.

Examples:

Input: num = 25
Output: 5.0

Input: -729
Output: NaN

Below programs illustrate the use of java.lang.StrictMath.sqrt() method: Program 1: 

Java




// 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);
    }
}


Output:

square root = 17.0
square root = NaN
square root = 0.0
square root = 23.406622994357814

Program 2: 

Java




// 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);
    }
}


Output:

square root = 34.058772731852805
square root = NaN
square root = -0.0
square root = 12.0
square root = NaN


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads