0

I hope that this is not a duplicate, but I've looked through many different posts, including Java Math.pow(a,b) time complexity, which do not directly answer the question for cases of pure nth roots. Since there are many different methods to quickly approximate nth roots: slow but reliable Binary Search O(log(n)), "Shifting nth root" and "The Newton–Raphson method", I'm curious whether or not Java's Math.pow() utilizes any of these, or does something entirely different? Again, not sure if this belongs somewhere else (software engineering stack exchange or math stack exchange) but I'm genuinely curious as I've always just trusted that Math.pow() will get me an answer efficiently and have put off implementing my own version of the Newton-Raphson method for (marginally) faster evaluation.

As I mentioned before, I looked over many posts in StackOverflow and SoftwareEngineering and have not found any answers to the problem. In my class, I recently learned about these different algorithms to calculate nth roots so Math.pow() must implement one of them, right?

Contone
  • 45
  • 4
  • 1
    Question was closed so my abbreviated answer is here: Looking at the source code for Math.pow takes you to StrictMath.pow which takes you to a Pow inner class which implements the 'compute' method. In there you can explore the specifics regarding the algorithm they currently follow. – Oscar Nov 10 '22 at 20:41
  • 3
    The source code is available at [Github](https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/FdLibm.java#L303) - have fun! (from the top of that class: "Port of the "Freely Distributable Math Library", version 5.3, from C to Java.") – user16320675 Nov 10 '22 at 22:04
  • Thanks everyone! I didn't realize that powers were handled the same in all languages. Also @user16320675, thanks for the link, I'm definitely going to spend the day looking through it! – Contone Nov 11 '22 at 17:08
  • Without ever looking at actual implementations, I bet that the computation is made the same way (using logarithms) for rational and irrational powers. Except for 1/2 and -1/2 and, who knows, 1/3. The relatively low usefulness of these specific exponents does probably not balance the added complexity. –  Nov 14 '22 at 12:34
  • And as Louis commented (answer deleted), there is an inescapable reason: the exponent argument of `pow` is a floating-point number; that does not allow to specify fractions. –  Nov 14 '22 at 13:17

0 Answers0