Table of contents
1.
Introduction
2.
What is Math pow() Function in Java?
3.
Signature of Java Math pow() 
4.
Syntax of Math pow() function in Java
4.1.
Explanation
5.
How Do I Use the Math Pow() Function in Java?
6.
Example of Math Pow() function in Java
7.
Some Special Cases of Power Function in Java
7.1.
Second Argument as Zero
7.2.
Second Argument as NaN
7.3.
Second Argument as Negative
8.
How to Return Integer type Value Using the Power Function?
9.
Frequently Asked Questions
9.1.
How do you write 2 power n in Java?
9.2.
What does Math POW () return?
9.3.
What is Math pow with 3 arguments?
9.4.
Is there any alternative to the power function in Java?
10.
Conclusion
Last Updated: Oct 7, 2024
Easy

Math pow() Function in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Understanding the Math.pow() function in Java can be challenging for many developers. However, fear not – we're here to assist you in navigating the intricacies of this function, which can be likened to a robust calculator, simplifying complex mathematical operations and offering a versatile tool for various tasks.

power function in java

In many languages, the inbuilt power function is not so powerful, but it can handle several edge cases without failing in Java. Let's discuss them in detail.

What is Math pow() Function in Java?

pow() function in Java is used to raise one number to the power of another. It takes two arguments: the base and the exponent, and returns the result as a double, with the syntax double result = Math. pow(base, exponent. 

The power function in Java returns the value of the first argument raised to the power of the second argument. It takes two arguments as parameters, and by default, it returns data type double. Unlike other programming languages, the power function in Java works almost like actual maths operations as it is not just limited to finding exponents. They can handle negative and decimal numbers as well.  

Signature of Java Math pow() 

public static double pow(double base, double exponent)
You can also try this code with Online Java Compiler
Run Code

Syntax of Math pow() function in Java

Math.pow(a,b) 
You can also try this code with Online Java Compiler
Run Code

The syntax Math.pow(a, b) refers to a function where a and b are arguments. a is the base number, and b is the exponent. The function calculates and returns the value of a raised to the power of b. For example, if a is 2 and b is 3, it calculates 

Explanation

The above function takes two arguments, usually of double data type. The above function returns base^exponent as output. Here a is the base, and b is the exponent. Its time complexity is O(log b), and space complexity is O(1).

Also see, Swap Function in Java

How Do I Use the Math Pow() Function in Java?


In Java, the power function is used through `Math.pow(double a, double b)`, where `a` is the base and `b` is the exponent. It returns `a` raised to the power of `b`. For example, `Math.pow(2, 3)` returns 8.0, as it computes (23).

Example of Math Pow() function in Java

In the below example, we have shown some different combinations of base and exponents for better understanding.

public class NinjaPower {
    public static void main(String[] args) {
        
        // Case 1
        double base = 2;
        double exponent = 3;
        double result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
        
        // Case 2
        base = 2;
        exponent = 0.5;
        result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
        
        // Case 3
        base = 0.2;
        exponent = 3;
        result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

2.0 to the power of 3.0 is 8.0
2.0 to the power of 0.5 is 1.4142135623730951
0.2 to the power of 3.0 is 0.008000000000000002

Some Special Cases of Power Function in Java

There are some peculiar cases when the argument has different values. Let’s discuss them:

Second Argument as Zero

If the second argument is a positive or negative zero, then the power function returns 1.0.

Code

class CodingNinja {
    public static void main(String[] args) {
        double base = 5.0;
        double exponent = -0;
        double result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

5.0 to the power of 0.0 is 1.0

Second Argument as NaN

If the second argument is not a number (NaN), then the power function also returns NaN.

Code

class CodingNinja {
    public static void main(String[] args) {
        double base = 5.0;
        double exponent = Double.NaN;
        double result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output

5.0 to the power of NaN is NaN

Second Argument as Negative

If the second argument is a negative number, then the power function solves it just like general maths, i.e., a^(-x) becomes (1/a)^x.

Code

class CodingNinja {
    public static void main(String[] args) {
        double base = 5.0;
        double exponent = -2;
        double result = Math.pow(base, exponent);
        System.out.println(base + " to the power of " + exponent + " is " + result);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

5.0 to the power of -2.0 is 0.04

How to Return Integer type Value Using the Power Function?

To return an integer value using the power function in Java, first use `Math.pow()` to compute the power, which returns a double, and then cast the result to an integer. For example, `(int) Math.pow(2, 3)` calculates (23) as 8.0 and then casts it to 8, an integer.

Example :

public class Main {
   public static void main(String[] args) {
       int base = 2;
       int exponent = 3;
       int result = (int) Math.pow(base, exponent); // Calculates 2^3 and casts the result to an integer
       System.out.println("Result: " + result); // Outputs: Result: 8
   }
}
You can also try this code with Online Java Compiler
Run Code

In this code, Math.pow(2, 3) computes 2323 and returns 8.0 as a double. The (int) before Math.pow() casts this double to an integer, making result equal to 8.

Frequently Asked Questions

How do you write 2 power n in Java?

In Java, to write 2 raised to the power of n, you use `Math.pow(2, n)`. This function takes 2 as the base and n as the exponent, computing and returning (2n). For example, `Math.pow(2, 3)` returns 8.0.

What does Math POW () return?

The `Math.pow()` function in Java returns a double value representing the result of raising the first argument (base) to the power of the second argument (exponent). For example, `Math.pow(2, 3)` returns 8.0, which is (23).

What is Math pow with 3 arguments?

The Python `pow()` function with three arguments, `pow(base, exponent, modulus)`, computes the power and then applies the modulus, returning the remainder of the power divided by the modulus. This is useful for modular exponentiation, especially in cryptography.

Is there any alternative to the power function in Java?

We can use the exponentiation operator (**) or implement the power function using a loop. The Math.pow() method is generally the simplest and most efficient way to perform power functions in Java.

Conclusion

In this article, we discussed the Power function in Java. We also saw different examples and some peculiar cases and their output. The power function is provided in almost all major programming languages, but they should be coded correctly, and the edge cases should be kept in mind.

If you liked this, read our other related blogs:

Live masterclass