@Blindy talks about possible approaches that Java could take in implementing pow.

First of all, the general case cannot be repeated multiplication. It won't work for the general case where the exponent is not an integer. (The signature for pow is Math.pow(double, double)!)

In the OpenJDK 8 codebase, the native code implementation for pow can work in two ways:

  • The first implementation in e_pow.c uses a power series. The approach is described in the C comments as follows:

    Copy* Method:  Let x =  2   * (1+f)
    *      1. Compute and return log2(x) in two pieces:
    *              log2(x) = w1 + w2,
    *         where w1 has 53-24 = 29 bit trailing zeros.
    *      2. Perform y*log2(x) = n+y' by simulating multi-precision
    *         arithmetic, where |y'|<=0.5.
    *      3. Return x**y = 2**n*exp(y'*log2)
    
  • The second implementation in w_pow.c is a wrapper for the pow function provided by the Standard C library. The wrapper deals with edge cases.

Now it is possible that the Standard C library uses CPU specific math instructions. If it did, and the JDK build (or runtime) selected1 the second implementation, then Java would use those instructions too.

But either way, I can see no trace of any special case code that uses repeated multiplication. You can safely assume that it is O(1).


1 - I haven't delved into how when the selection is / can be made.

Answer from Stephen C on Stack Overflow
Top answer
1 of 2
13

@Blindy talks about possible approaches that Java could take in implementing pow.

First of all, the general case cannot be repeated multiplication. It won't work for the general case where the exponent is not an integer. (The signature for pow is Math.pow(double, double)!)

In the OpenJDK 8 codebase, the native code implementation for pow can work in two ways:

  • The first implementation in e_pow.c uses a power series. The approach is described in the C comments as follows:

    Copy* Method:  Let x =  2   * (1+f)
    *      1. Compute and return log2(x) in two pieces:
    *              log2(x) = w1 + w2,
    *         where w1 has 53-24 = 29 bit trailing zeros.
    *      2. Perform y*log2(x) = n+y' by simulating multi-precision
    *         arithmetic, where |y'|<=0.5.
    *      3. Return x**y = 2**n*exp(y'*log2)
    
  • The second implementation in w_pow.c is a wrapper for the pow function provided by the Standard C library. The wrapper deals with edge cases.

Now it is possible that the Standard C library uses CPU specific math instructions. If it did, and the JDK build (or runtime) selected1 the second implementation, then Java would use those instructions too.

But either way, I can see no trace of any special case code that uses repeated multiplication. You can safely assume that it is O(1).


1 - I haven't delved into how when the selection is / can be made.

2 of 2
6

You can consider Math.pow to be O(1).

There's a few possible implementations, ranging from a CPU assembler instruction (Java doesn't use this) to a stable software implementation based on (for example) the Taylor series expansion over a few terms (though not exactly the Taylor implementation, there's some more specific algorithms).

It most definitely won't repeatedly multiply if that's what you're worried about.

🌐
Reddit
reddit.com › r/learnprogramming › time complexity using math.pow in for loop
r/learnprogramming on Reddit: Time complexity using Math.Pow in for loop
November 28, 2017 -
for (int i = 0; Math.pow(2,i) < N; i++)
    sum += i;

I'm struggling understanding the time complexity when Math.Pow is in the loop condition. What is worst case time complexity in the for loop? O(log(N))?

Top answer
1 of 2
1

Answering that question depends on what you consider the complexity of Math.pow(). For most hardware, it's O(1), but on a theoretical Turing machine, it isn't. Let's assume it's free.

Let's consider the loop. The complexity is going to be a function describing how many times the loop runs, given N. The loop starts with i=0, adds 1 to i each time, and continues while Math.pow(2,i) < N. Let's consider some cases.

N is 0, loop runs 0 times.

N is 1, loop runs 1 time because the second time, 2^1 > 1.

N is 2, loop runs 2 times, 2^2 > 2.N is 3, loop runs 2 times, 2^2 > 3.

N is 4, loop runs 3 times, 2^3 > 4.

N is 5, loop runs 3 times, 2^3 > 5.

N is 6, loop runs 3 times, 2^3 > 6.

N is 7, loop runs 3 times, 2^3 > 7.

N is 8, loop runs 4 times, 2^4 > 8

N is 100, loop runs 7 times, 2^7 > 100

N is 1000, loop runs 10 times, 2^10 > 1000

Okay, so a function that describes this growth looks to be the opposite of taking an exponent. That's logarithms. O(log(N)) is probably right. Let's verify, remembering that in CS, we tend to assume log in base-2.

N is 2, loop runs 2 times, log(2) is 1

N is 3, loop runs 2 times, log(3) is ~1.5

N is 4, loop runs 3 times, log(4) is 2

N is 5, loop runs 3 times, log(5) is ~2.3

N is 6, loop runs 3 times, log(6) is ~2.5

N is 100, loop runs 7 times,log(100) is ~6.6

N is 1000, loop runs 10 times, log(1000) is ~9.9

Yep, it's O(log(N)).

All that said, I already knew it would be O(log(N)) because of a useful shortcut. If your for loop iterates through every value from 1 to N, that's always going to be O(N). If your for loop does N times N times, that's O(N^(2)). And, most importantly, if your loop gets halfway to the end with every step, that's O(log(N)) for sure. Walking twice as far with every step to a target is the same thing as walking a number down to zero by removing a bit each time, which is the same thing as walking halfway to your goal (removing a bit is the same as dividing by 2).

2 of 2
1

So your loop runs while 2i < N.

Take the log base 2 of both sides of the inequality and you learn that your loop runs while i < log(N).

So, the answer to the question "How many times does this loop run?" is O(log N).

The time complexity is a slightly trickier question, because Math.powmay not be a constant time operation. I can't find any definitive answer about that. However, if this is a homework question you are almost certainly expected to assume that Math.pow is constant time.

🌐
Topcoder
topcoder.com › thrive › articles › calculate-pow-x-n
Calculate Pow(x, n)
Time Complexity: O(N) because we are iterating once till N. Space Complexity: O(1) No extra space has been used. We will call the pow(x,n) method for calculating and the same method will call itself passing x and n-1.
🌐
Medium
medium.com › @AlexanderObregon › javas-math-pow-method-explained-7c0f746ad420
Understanding Java's Math.pow() Method | Medium
July 16, 2024 - Internal Computation: The Math.pow() method computes the result of raising the growth factor (1.05) to the power of 10. This involves using complex mathematical operations, such as logarithms and exponentials, to accurately calculate the result.
🌐
Stack Overflow
stackoverflow.com › questions › 61561299 › time-complexity-of-math-powa-n-java
performance - Time complexity of Math.pow(a,n) JAVA - Stack Overflow
What is the time complexity of math.pow(a,n) function in java? Because when we solve the same using recursion, the time complexity is O(n).
🌐
Quora
quora.com › What-is-the-code-behind-the-math-pow-function
What is the code behind the math.pow function? - Quora
Answer (1 of 4): [code]Public static int pow(int num, int pow) { int ans =num; if (pow ==1) { return num; } else if (i==0) { return 1; } for(int i =1, i!= pow, i++) { ans = ans*num; } return ans; } // I think this is it. This code does not deal // with negative exponents or frac...
🌐
Google Groups
groups.google.com › g › golang-nuts › c › KHFxwsO_vUA
What is the time complexity of math.Pow function?
So the time complexity for math.Pow(0.87, 16384) should be O(log2(16384)) = (log2(2^14)) = O(14).
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java numbers › using math.pow in java
Using Math.pow in Java | Baeldung
January 8, 2024 - Learn how to use the Java's Math.pow() method to calculate the power of any given base quickly.
🌐
Algorithmexamples
java.algorithmexamples.com › web › Maths › Pow.html
2000+ Algorithm Examples in Python, Java, Javascript, C, C++, Go, Matlab, Kotlin, Ruby, R and Scala
If n is even, the algorithm computes ... multiplications required to compute the final result, resulting in a more efficient algorithm with a time complexity of O(log n). package Maths; //POWER (exponentials) Examples (a^b) public cla...
🌐
Quora
quora.com › Should-I-use-Math-pow-to-square-numbers-in-Java
Should I use Math.pow to square numbers in Java? - Quora
Answer (1 of 5): DO NOT USE Math.Pow TO CALCULATE A SQUARE! Here is a test (in C# not Java but I expect the result will be similar). [code]private static void PowTest() { const int N = 100000000; double[] x = new double[N]; double[] y = new double[N]; double[] z = new double[N]; Ran...
🌐
IronPDF
ironpdf.com › ironpdf for java › ironpdf for java blog › java pdf tools › math.pow java
Math.Pow Java (How It Works For Developers)
March 27, 2024 - The method's simplicity and adherence to standard mathematical notation contribute to its user-friendly nature, making it accessible for developers seeking to perform exponentiation in their Java programs. Utilizing Math.pow() is straightforward, as it returns the result of raising the base to the power of the exponent in the form of a double value. It allows developers to perform exponentiation without manually implementing complex mathematical algorithms.
🌐
GeeksforGeeks
geeksforgeeks.org › java › math-pow-method-in-java-with-example
Math pow() Method in Java with Example - GeeksforGeeks
March 28, 2025 - Example 1: This example demonstrates how to use the Math.pow() method in Java to calculate the power of a number (base raised to the exponent).
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › write-a-c-program-to-calculate-powxn
Write program to calculate pow(b, e) - GeeksforGeeks
// Java program to calculate pow(b, e) class GfG { // Naive iterative solution to calculate pow(b, e) static double power(double b, int e) { // Initialize result to 1 double pow = 1; // Multiply b for e times for (int i = 0; i < Math.abs(e); i++) pow = pow * b; if (e < 0) return 1 / pow; return pow; } public static void main(String[] args) { double b = 3.0; int e = 5; double res = power(b, e); System.out.println(res); } } Python ·
Published   July 23, 2025
🌐
WsCube Tech
wscubetech.com › resources › java › programs › power-number
Java Program to Calculate the Power of a Number (7 Ways)
October 19, 2025 - Explore 7 different Java programs to calculate the power of a number. Learn methods using for loops, recursion, Math.pow(), and more. Ideal for learners!
🌐
CodeGym
codegym.cc › java blog › java math › math pow() method in java
Math.pow() Method in Java
December 5, 2024 - Moreover, if we are going to calculate ... 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 ...
🌐
Scaler
scaler.com › topics › javascript-math-pow
JavaScript Math.pow() Function - Scaler Topics
December 12, 2022 - The Math.pow() function is an in-built function in JavaScript that returns a number equal to the base number raised to its exponent. It takes two parameters - the base and the exponent. The time complexity of the function is O(1).