# Optimized Approximative pow() in C / C++

Mostly thanks to this reddit discussion, I have updated my pow() approximation for C / C++. I have now two different versions:

This new code uses the union trick, instead of the weird casting trick I’ve used before. This means that -fno-strict-aliasing is no more required any more when compiling, and it is also a bit faster because one less temporary variables is needed. When you have a little endian machine, you have to exchange u.x[0] and u.x[1]. On my PC, this version is 4.2 times faster than the much more precise pow().

Besides that, I also have now a slower approximation that has much less error when the exponent is larger than 1. It makes use exponentiation by squaring, which is exact for the integer part of the exponent, and uses only the exponent’s fraction for the approximation:

This code is 3.3 times faster than pow(). Writing a microbenchmark is not easy, so I have posted mine here. Here is also a Java version of the more accurate pow approximation.

Any ideas how this could be improved? Please post them!

11 Comments on "Optimized Approximative pow() in C / C++"

Notify of

[…] I was hunting for a faster one. There are quite some interesting variations around in the net, like one for doubles which is quite some impressive a bit-hackery, reminds me of Quake’s sqrt(). The […]

Guest

The new approximation does not work with negative exponents because the loop will never end. Is the first approximation valid for negative exponents?

Guest
inlineWillIncreaseit

I have seen some people use the old fashion Taylor aproximations in they calculus but there are some better algorithms.
Don’t they use some asm and some tables for that

[…] ?????? double? ????? ??? ?? ??? Optimized Approximative pow() in C / C++ | Martin Ankerl? fastPow ??? ???? ???, ??? […]

Guest

You could modify the function like this so it can handle negative exponents:

inline double CompOgdenRobust::fastPrecisePow(double a, double b)
{
if (b >= 1;
}

return r * u.d;
}

Guest

apologies I can’t seem to post the code in the comments without it messing up

Guest