Square Root

Math Class

Square Root

🙋 Need help? Ask an expert now!

One of the more popular Math class functions is the square root function. Like the floor() and ceil() methods, the square root method takes and returns the double datatype. It can be written as follows, Math.sqrt(25), and returns the rounded positive square root. Review the examples below.

package exlcode;

public class SquareRootExample {

  public static double exampleVariableOne = Math.sqrt(100);
  public static double exampleVariableTwo = Math.sqrt(6.25);
  public static double exampleVariableThree = Math.sqrt(3);

  public static void main(String[] args) {

This method will return a double number even if the result is a perfect square. The error that will occur if we use a number less than zero is a result of "NaN", which stands for "Not a Number". Therefore, if we were to write Math.sqrt(-4), the return will be "NaN". Imaginary numbers cannot be represented or returned using this method. And lastly, keep in mind that this function will only return one positive square root value.

Edit Me on GitHub!

Application Question

Math.sqrt(16) returns