Home » Java programming language

Java StrictMath sqrt() Method with Example

StrictMath Class sqrt() method: Here, we are going to learn about the sqrt() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on January 05, 2020

StrictMath Class sqrt() method

  • sqrt() Method is available in java.lang package.
  • 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.

Syntax:

    public static double sqrt(double d);

Parameter(s):

  • double d – represents for which we have to find the square root.

Return value:

The return type of the method is double, it returns the square root of the given parameter.

Note:

  • If we pass NaN, the method returns the same (i.e. 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



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.