Fast implementation/approximation of pow() function in C/C++

后端 未结 3 974
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 22:37

I m looking for a faster implementation or good a approximation of functions provided by cmath.

I need to speed up the following functions

相关标签:
3条回答
  • 2021-01-11 23:24

    Here are some approxmiations:

    • Optimized pow Approximation for Java and C / C++. This approximation is very inaccurate, you have to try for yourself if it is good enough.
    • Optimized Exponential Functions for Java. Quite good! I use it for a neural net.

    If the above approximation for pow is not good enough, you can still try to replace it with exponential functions, depending on your machine and compiler this might be faster:

    1. x^y = e^(y*ln(x))
    2. And the result: e^(z * x^y) = e^(z * e^(y*ln(x)))

    Another trick is when some parameters of the formula do not change often. So if e.g. x and y are mostly constant, you can precalculate x^y and reuse this.

    0 讨论(0)
  • 2021-01-11 23:27

    What are the possible values of x and y? If they are within reasonable bounds, building some lookup tables could help.

    0 讨论(0)
  • 2021-01-11 23:27

    I recommend the routines in the book "Math Toolkit for Real-Time Programming" by Jack W. Crenshaw.

    You might also want to post some of your code to show how you are calling these functions, as there may be some other higher level optimisation possibilities that are not apparent from the description given so far.

    0 讨论(0)
提交回复
热议问题