Java Math.pow() Method with Examples

Oct 26, 2016 · 4 mins read

The Math.pow(a, b) is a built-in method of the Math class that’s used for calculating the power of a given number.

The power of a number defines the result that we get if we multiply the number with itself. For example, to find 10 to the power of 4, we multiply 10 with itself 4 times: 10^4 = 10 x 10 x 10 x 10 = 10000.

This Math.pow() method accepts two arguments and returns the value of its first argument raised to the power of the second argument. There are some special cases that we need to be aware of:

  • If the second argument is positive or negative zero, then the result is 1.0.
  • If the second argument is 1.0, then the result is the same as the first argument.
  • If the second argument is NaN, then the result is NaN.
  • If the first argument is NaN and the second argument is nonzero, then the result is NaN.
  • If
    • the absolute value of the first argument is greater than 1 and the second argument is positive infinity, or
    • the absolute value of the first argument is less than 1 and the second argument is negative infinity,then the result is positive infinity.

Syntax of the Math.pow Method

static double pow(double a, double b)

This is a static method like all other methods of the Math class and can be called directly on the Math class as Math.pow(...).

Arguments:

  • the first parameter, a is the base.
  • the second parameter, b is the exponent.

Return Value:

  • This method returns a^b or a raised to the power b as a double value.

a-raised-to-power-of-b

Code Example Showing Math.pow() In Action

Here’s a simple example where we raise 3 to the power of 2, or 3^2, and convert the result back to an int, getting 9.

int result = (int)Math.pow(3, 2) // result = 9

Here’s a complete example that also shows the special case when the second argument is NaN.

import static java.lang.Double.NaN;

public class Main {
    public static void main(String[] args) {
        System.out.println("10^4 = " + (long) Math.pow(10, 4)); // 10000
        System.out.println("2^4 = " + (long) Math.pow(2, 4));   // 16
        System.out.println("2^1 = " + (long) Math.pow(2, 1));   // 2
        System.out.println("2^0 = " + (long) Math.pow(2, 0));   // 1
        // If the second argument is NaN, then the result is NaN.
        System.out.println("2^NaN = " + Math.pow(2, NaN));      // 0

        System.out.println("2.5^3 = " + Math.pow(2.5, 3));      // 15.625
    }
}

Output

10^4  = 10000
2^4   = 16
2^1   = 2
2^0   = 1
2^NaN = NaN
2.5^3 = 15.625

There you have it. Hope you found this tutorial useful!

See also

It’s worth noting that the Math class in Java contains several useful methods for arithmetic, logarithms and trigonometric operations. The following YouTube video below talks about several methods of the Math class with examples. It’s a short video and I recommend that you watch it.

#math-class #java

You May Also Enjoy


If you like this post, please share using the buttons above. It will help CodeAhoy grow and add new content. Thank you!