The Java language is equipped with a comprehensive library for mathematical functions and operations. It is called the "Math" class, and it resides in the java.lang package. The Math library includes methods for fundamental numerical operations, trigonometry, finding min-max, generating random numbers, and logarithmic operations.
In today's tutorial, we will focus on the Math.pow() method, which is used to calculate powers of numbers using a base and an exponent. Let's get started.
The expression ‘nth power of a’ can be mathematically written as a

^{n}. We can define the terms in the expressions as follows. a - base n - exponent Let's consider the exponent. We can identify it as the number of times a multiplication operation is repeated. The base is the number that gets multiplied by itself.Let us now create a simple Java method to calculate the power of a number. Please note that in the following example, we are passing two arguments into the powerFunction() method, which are the base and the exponent, respectively.```
public class Main {
public static void main(String[] args) {
int result = powerFunction(2, 5);
System.out.println("2 raised to the power of 5 = " + result);
}
static int powerFunction(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result = base * result;
}
return result;
}
}
```

If we run the above example code, it would return 32. You might be imagining why we cannot simply write base^exponent. We can't do that because Java has no exponentiation operator that we can use in a single line of code.
Considering the above code, it’s obvious that we had to spend quite a bit of time writing this method. Moreover, if we are going to calculate high powers, the method will take a considerable amount of time to complete these calculations since we are using for loops. In addition to that, loops will prevent us from performing power calculations with fractional exponents. And this method doesn't have good computational complexity, because there is not very favorable as there is more room for optimization.
Considering how frequent exponentiation and other mathematical operations are used by programmers, back in the day Java's developers created a library called ‘Math’, which is dedicated to facilitating mathematical operations. Therefore, instead of writing a power function from scratch, we can take advantage of this library that is included in the Java Lang package.## What is the Math.pow method and how do we use it?

Math.pow can be found in the java.lang package as a method of the Math library. It is used to calculate the power of numbers, both integers as well as doubles. Let’s consider its syntax.```
public static double pow(double base, double exponent)
```

As we can see in the syntax, the java.lang.Math.pow() method takes in two arguments. The first one is the base and the second one is the exponent. This will return base^{exponent}as its output. Let’s see how we can call it.## Raising a Number to a Power in Java using Math.pow

Let's find the value of 5^{4}using Math.pow.```
import java.lang.Math;
public class MyClass{
public static void main(String []args){
double answer = Math.pow(5, 4);
// java.lang.Math.pow() method
System.out.println("5 raised to the power of 4 = " + answer);
}
}
```

The output is 625.0. As you can see, it’s a double value. If you are bothered by the decimal point, we can easily get rid of it by casting the number to an integer as follows. Note that we are changing the first line inside the main method.
```
int answer = (int) Math.pow(5, 4);
```

Now the result is 625.
Let’s use fractional numbers for both base and exponent and try to get an answer. Let’s see what the value of 1.25^{4.5}is.```
import java.lang.Math;
public class MyClass {
public static void main(String[] args) {
double answer = Math.pow(1.25, 4.5);
// java.lang.Math.pow() method
System.out.println("1.25 raised to the power of 4.5 = " + answer);
}
}
```

This would output 2.729575167846423.
If you use a calculator, you will see that it returns the same answer.
Let’s go through one more example before we move onto the next section. We will raise a number to a negative power and compare the results. For this example, we will pick 4 as the base and -2 as the exponent.
```
import java.lang.Math;
public class MyClass{
public static void main(String []args){
double answer = Math.pow(4, -2);
// java.lang.Math.pow() method
System.out.println(answer);
}
}
```

We get the output 0.0625.## Quick Example: How to Round Your Answer

Say, we need to find the value of 1.25^{4.5}. The answer is 2.729575167846423. Quite often it is necessary to round the result. Let's try to get an answer accurate to the fourth decimal place. How to do it? What if we only require the first 4 decimal places? We can use the java.lang.Math.round method for that. However, as Math.round rounds the value to the nearest integer, we will have to multiply it by the number of decimal places and then round and divide again.```
import java.lang.Math;
public class MyClass{
public static void main(String []args){
double answer = Math.pow(1.25, 4.5);
answer = Math.round(answer*100.0)/100.0;
System.out.println(answer);
}
}
```

The output is 2.73.## How to Use Math.pow Properly

When we use java.lang.Math.pow method, there are a few things to keep in mind.- If the exponent parameter is zero, the output will be 1.0. This is because the power of zero for any number is defined as one.
- If the exponent parameter is one, the output will be the base parameter. This is because if you raise any number to the power of 1, the result is the same as the base.
- If the base is negative/positive zero and the exponent parameter is a negative number, then the result is Infinity. (Negative zeros can occur due to the rounding of numbers between zero and the smallest representable negative non-zero number).
- If the exponent parameter is NaN, the output will also be NaN.

```
import java.lang.Math;
public class MyClass{
public static void main(String []args){
double base = 5;
double exponent = Double.NaN;
double answer = Math.pow(base, exponent);
System.out.println(answer);
}
}
```

This will output NaN. So, if your code results in NaN, it would be wise to check whether the exponent argument is NaN.
In case you are wondering what NaN is, it means ‘not a number’ and indicates that the value has not been defined.
We believe that you are all now set to go ahead and put lang.Math.pow() to use in your applications.
GO TO FULL VERSION