Quick exponentiation: real ^ real (C ++ MinGW, Code :: Blocks)

I am writing an application where in a certain block I need to increase the number of reals by about 3 * 500 * 500 times. When I use the exp (y * log (x)) algorithm, the program lags noticeably. It is much faster if I use another algorithm based on a game with data types, but this algorithm is not very accurate, although it provides decent results for modeling, and it is still not ideal in terms of speed.

Is there any exact exponentiation algorithm for real powers faster than exp (y * log (x))?

Thanks in advance.

+4
source share
2 answers

If you need good accuracy and you don’t know anything about the distribution of bases (x values) a priori, then pow (x, y) is the best portable answer (on many - not all) platforms, it will be faster than exp (y * log (x)), and it’s also better to behave numerically). If you know something about what ranges of x and y may lie, and with what distribution this will be a big help for people who are trying to give advice.

The usual way to do this faster while maintaining good accuracy is to use a library procedure designed to perform many of these calculations simultaneously for an array of x values ​​and an array of y values. The trick is that such library implementations tend to cost money (e.g. Intel MKL) or are platform dependent (e.g. vvpowf in Accelerate.framework on OS X). I don't know much about MinGW, so someone else will need to tell you what is there. GSL may have something in this direction.

+5
source

Depending on your algorithm (in particular, if you have few additions), sometimes you can leave work (at least partially) in the log space. You probably already considered this, but if your intermediate representation is log_x and log_y, then log (x ^ y) = exp (log_y) * log_x, which will be faster. If you can even be selective, then obviously calculating log (x ^ y) with y * log_x is even cheaper. If you can avoid even a few performances, you can win a lot of work. If there is any way to rewrite all the loops that you must get in order to perform exposure operations outside of the innermost loop, this will lead to pretty high performance.

+4
source

All Articles