Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - sqrt() method



The sqrt() method returns the square root of the argument.

Syntax

double sqrt(double value)
double sqrt(float value) 
double sqrt(int value) 
double max(long value)

Parameters

  • value − A primitive data type.

Return Value

This method returns the square root of the argument.

Example - Usage of sqrt method for double value

Following is an example of the usage of this method −

Example.groovy

class Example { 
   static void main(String[] args){ 
      double x = 11.635; 

      printf("sqrt(%.3f) is %.3f%n", x, Math.sqrt(x));
   } 
}

Output

When we run the above program, we will get the following result −

sqrt(11.635) is 3.411

Example - Usage of sqrt method for int value

Following is an example of the usage of this method −

Example.groovy

class Example { 
   static void main(String[] args){ 
      int d = 12; 
		
      println(Math.sqrt(d)); 
   } 
}

Output

When we run the above program, we will get the following result −

3.4641016151377544

Example - Usage of sqrt method for float value

Following is an example of the usage of this method −

Example.groovy

class Example { 
   static void main(String[] args){ 
      float d = 12.123f;
		
      println(Math.sqrt(d)); 
   } 
}

Output

When we run the above program, we will get the following result −

3.481809895005541
groovy_numbers.htm
Advertisements