Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - pow() method



The pow() method returns the value of the first argument raised to the power of the second argument.

Syntax

double pow(double base, double exponent)
double pow(float base, float exponent)
double pow(int base, int exponent)
double pow(long base, long exponent)

Parameters

  • base − A primitive data type.

  • exponent − A primitive data type.

Return Value

This method returns the value of the first argument raised to the power of the second argument.

Example - Usage of pow() method for double values

Following is an example of the usage of this method −

Example.groovy

class Example {
   static void main(String[] args) {
      double x = 11.635;
      double y = 2.76; 
   
      printf("The value of e is %.4f%n", Math.E);
      printf("pow(%.3f, %.3f) is %.3f%n", x, y, Math.pow(x, y));  
   } 
}

Output

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

The value of e is 2.7183 
pow(11.635, 2.760) is 874.008

Example - Usage of pow() 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; 
	  int e = 2;
		
      println(Math.pow(d, e)); 
   } 
}

Output

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

144.0

Example - Usage of pow() 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.0f; 
	  float e = 2.0f;
		
      println(Math.pow(d, e)); 
   } 
}

Output

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

144.0
groovy_numbers.htm
Advertisements