"The power of 2" is squaring. You'd be better off doing that by multiplying the number by itself.

The library version of sqrt is probably faster than anything you could dig up elsewhere. If you call a C routine, you'll just add overhead from the cross-language call. But do you need accurate square roots, or would a table lookup of approximations do? Do the values repeat a lot, i.e. do you often need to calculate the roots of the same numbers? If so, caching the square roots in a HashMap might be faster than computing them.

Answer from Carl Smotricz on Stack Overflow
🌐
Quora
quora.com › What-is-an-alternative-to-Math-pow-in-Java
What is an alternative to Math.pow in Java? - Quora
Answer (1 of 4): Yes you can write your function for this, [code]double power(double d, int n){ double p=1; for(int i=0;i
🌐
Reddit
reddit.com › r/learnprogramming › [java] calculate x^n without math.pow(x,n) and with an array?
r/learnprogramming on Reddit: [Java] Calculate X^n without Math.pow(x,n) and with an array?
January 8, 2017 -

I've been trying to calculate the Xn without using math.pow in order to compare it with other algorithms to test their performance and math.pow is too optimized so it will impede me to compare it successfully with other algorithms:

I will use the Xn method to use it within a bruteforce method that evaluates a polynomial at x.

This method will be compared with my horners method.

If I do it using math.pow(x,n) the bruteforce will be faster than horners sometimes and I presume that its because math.pow being too fast.

Here are both method to compare, can you shed some light on why sometimes bruteforce is faster than horners, is it because of math.pow?

    //Metodo de Horner:
 public double horners(int[] coef_array, double x) {
    double resultado = 0;
    int c = 0;
    for (int i = coef_array.length - 1; i >= 0; i--) {
        resultado = (resultado * x) + coef_array[i];
        c++;
    } //Evalua en x; 
    set_cant_mult_H(c);
    return resultado;
}

//Metodo "obvio", "a pie" o "bruteforceado":
public double brute(int[] coef_array, double x) {
    double resultado = 0;
    int c = 0;
    for (int i = coef_array.length - 1; i >= 0; i--) {
        c++;
        resultado += coef_array[i] * Math.pow(x, i); // Change this mathpow with something slower?!
    }
    set_cant_mult_BF(c);
    return resultado;
}    

So to wrap it off, Horners bruteforce are compared but sometimes horners is slower than bruteforce and I presume its because math.pow(x,n) is too fast.

Remember I am using arrays so maybe the new pow method may need to receive an array!

🌐
GeeksforGeeks
geeksforgeeks.org › java › calculating-the-power-of-a-number-in-java-without-using-math-pow-method
Calculating the Power of a Number in Java Without Using Math pow() Method - GeeksforGeeks
March 2, 2021 - In Java, we can calculate the power of a number without using the predefined Math.pow() method. To calculate base^exponent, we need the base number and an exponent.
Find elsewhere
🌐
Quora
quora.com › Should-I-use-Math-pow-to-square-numbers-in-Java
Should I use Math.pow to square numbers in Java? - Quora
Java (programming languag... ... CS degree and many years of coding. · Author has 8.8K answers and 178.7M answer views · 7y · That wouldn’t be a reasonable choice in most circumstances I can think of. Using x*x will give you the square of x for any variable x with a numeric data type such as int or double, and the result will have the same type (which isn’t the case with Math.pow).
🌐
Ankerl
martin.ankerl.com › 2007 › 10 › 04 › optimized-pow-approximation-for-java-and-c-c
Optimized pow() approximation for Java, C / C++, and C#
This new approximation is about 23 times as fast as Math.pow() on my machine (Intel Core2 Quad, Q9550, Java 1.7.0_01-b08, 64-Bit Server VM). Unfortunately, microbenchmarks are difficult to do in Java, so your mileage may vary.
🌐
Royvanrijn
royvanrijn.com › blog › 2012 › 07 › java-speed-of-math
Java vs Javascript: Speed of Math
Lets see what changing Math.* to FastMath.* does to the performance: This is already much better. But still the method causing most delay is FastMath.pow(). Why is Javascript so much faster? The method is made so you can calculate the power of two doubles, not only integer values.
🌐
iO Flood
ioflood.com › blog › math-pow-java
Java's Math.pow() Function | Guide to Exponents in Java
February 29, 2024 - While Math.pow() is a powerful tool for exponentiation, it’s not the only way to perform this operation in Java. Let’s explore some alternative approaches, such as using loops or the BigInteger class.
🌐
Reddit
reddit.com › r/gamedev › fast approximation to math.pow()
r/gamedev on Reddit: Fast approximation to Math.pow()
September 6, 2011 -

Hi all, I have just updated my java version of the approximation for Math.pow(), which is now 30% faster than the previous version. On my machine (Core2 Quad Q9550, Java 1.7.0_01-b08, 64-Bit Server VM) this is now 130 times faster than Math.pow(), but a hell of a lot less precise.

here is the code:

public static double pow(final double a, final double b) {
    final long tmp = Double.doubleToLongBits(a);
    final long tmp2 = (long)(b * (tmp - 4606921280493453312L)) + 4606921280493453312L;
    return Double.longBitsToDouble(tmp2);
}

More info (and C / C++ / C# code) here. Depending on the range of values you want to use it for, the error can be quite high. You definitely need to test if it is good enough for your application. I personally have used it for simulation code, but I think it might be quite useful for games too.

UPDATE: Thanks to Madsy9's suggestion, a 3 times slower approximation (still 40 times faster than Math.pow) is now here: http://pastebin.com/ZW95gEyr This has 1.7% average error, no matter how large the exponent gets :)

🌐
DevDreamz
devdreamz.com › question › 202664-java-faster-alternative-to-math-pow-and-math-sqrt
Java - Faster alternative to Math.pow() and Math.sqrt() - DevDreamz
You are unlikely to find a better(faster) implementation than the Java Math one. You might have more luck trying to change the way you do the calculations in your algorithm.