Java Math.pow() method Usage Java Math.pow() method Usage

In this quick article, We’ll see usage of Java Math.pow() method which takes two arguments, a and b, and returns a to the power of b i.e. ab

Syntax

public double pow(double a, double b)

Where,

  • Parameter a is the base
  • Parameter b is the exponent
  • Returns ab

Example

// Returns 8.0
double result = (int) Math.pow(2, 3);

// Cast to int, returns 8
int intResult = (int) Math.pow(2, 3);

// Returns 117.29730800599916
double doubleResult = Math.pow(2.5, 5.2);

// Returns 1.0
double zeroPowerResult = Math.pow(2, 0);

// Returns 2.0
double sameResult = Math.pow(2, 1);

// Returns NaN
double nanResult = Math.pow(2, Double.NaN);

Points to note:-

  1. By default, returns the result in double
  2. You can cast the result to int
  3. Both arguments, base and exponent can have decimal points
  4. If second argument is zero then result will be 1.0
  5. If second argument is 1 then result will be value of first argument
  6. If second argument is NaN then result will be NaN

Why It’s 𝘖(1), Not a Loop or Recursion

Unlike a hand-written power function — such as the recursive M^N implementation in M Power N Using Recursive function, which takes 𝘖(n) or 𝘖(log n) depending on the algorithm — Math.pow() doesn’t loop or recurse over the exponent at all. It’s backed by a native/hardware (or StrictMath) floating-point implementation that computes the result directly, so a single call runs in 𝘖(1) regardless of how large the exponent is. That’s fine for double math, but it comes with the precision trade-offs below when you actually want an exact integer result.

More Edge Cases

  • Negative base, integer exponent — works as expected. Math.pow(-2, 3) returns -8.0.
  • Negative base, fractional exponent — always NaN. Math.pow(-8, 1.0 / 3.0) returns NaN, even though the real cube root of -8 is -2. Raising a negative number to a non-integer power isn’t a single-valued real operation, so Math.pow refuses rather than guessing. If you specifically need a real cube root (including of negative numbers), use Math.cbrt(-8), which correctly returns -2.0.
  • Math.pow(0, 0) is 1.0, not 0.0 or NaN — this matches the IEEE 754 / most-languages convention for 0^0.
  • A base of 1 does not special-case a NaN exponent. You might expect “anything except unresolved-NaN cases raised to a weird power is still itself when the base is 1”, but verified in Java, Math.pow(1, Double.NaN) still returns NaN, not 1.0.
  • Infinite exponents saturate. Math.pow(2, Double.POSITIVE_INFINITY) returns Infinity (a base greater than 1 diverges), while Math.pow(0.5, Double.POSITIVE_INFINITY) returns 0.0 (a base between 0 and 1 collapses toward zero).

Precision Limits for Large Integer Powers

Math.pow() always computes in double, and a double’s 52-bit mantissa can only represent integers exactly up to 253 (9007199254740992). Ask it for a large integer power beyond that range and it silently rounds off the low-order digits instead of erroring:

import java.math.BigInteger;

double approx = Math.pow(3, 40);
BigInteger exact = BigInteger.valueOf(3).pow(40);

System.out.println(approx); // 1.2157665459056929E19
System.out.println(exact);  // 12157665459056928801

Printed in full, approx is 12157665459056929000 — the last few digits don’t match the true value, ...928801. 3^30 (205891132094649, 15 digits) still round-trips exactly, so the rounding only shows up once the result outgrows double’s exact-integer range. For an exact large integer power, use BigInteger.pow(int) instead of Math.pow(), or the integer-only fast-exponentiation approach in M Power N Using Recursive function.