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:-
- By default, returns the result in double
- You can cast the result to int
- Both arguments, base and exponent can have decimal points
- If second argument is zero then result will be 1.0
- If second argument is 1 then result will be value of first argument
- 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)returnsNaN, even though the real cube root of-8is-2. Raising a negative number to a non-integer power isn’t a single-valued real operation, soMath.powrefuses rather than guessing. If you specifically need a real cube root (including of negative numbers), useMath.cbrt(-8), which correctly returns-2.0. Math.pow(0, 0)is1.0, not0.0orNaN— this matches the IEEE 754 / most-languages convention for0^0.- A base of
1does not special-case aNaNexponent. 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 returnsNaN, not1.0. - Infinite exponents saturate.
Math.pow(2, Double.POSITIVE_INFINITY)returnsInfinity(a base greater than 1 diverges), whileMath.pow(0.5, Double.POSITIVE_INFINITY)returns0.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.