Java.lang.Math.sqrt() Method

Advertisements


Description

The java.lang.Math.sqrt(double a) returns the correctly rounded positive square root of a double value. Special cases:

  • If the argument is NaN or less than zero, then the result is NaN.

  • If the argument is positive infinity, then the result is positive infinity.

  • If the argument is positive zero or negative zero, then the result is the same as the argument.

Otherwise, the result is the double value closest to the true mathematical square root of the argument value.

Declaration

Following is the declaration for java.lang.Math.sqrt() method

public static double sqrt(double a)

Parameters

  • a -- a value.

Return Value

This method returns the positive square root of a. If the argument is NaN or less than zero, the result is NaN.

Exception

  • NA

Example

The following example shows the usage of lang.Math.sqrt() method.

package com.tutorialspoint;

import java.lang.*;

public class MathDemo {

   public static void main(String[] args) {

      // get two double numbers numbers
      double x = 9;
      double y = 25;

      // print the square root of these doubles
      System.out.println("Math.sqrt(" + x + ")=" + Math.sqrt(x));
      System.out.println("Math.sqrt(" + y + ")=" + Math.sqrt(y));

   }
}

Let us compile and run the above program, this will produce the following result:

Math.sqrt(9)=3.0
Math.sqrt(25)=5.0



Advertisements
Advertisements