Fast Exp calculation: possible to improve accuracy without losing too much performance?

前端 未结 5 2090
难免孤独
难免孤独 2020-12-04 18:11

I am trying out the fast Exp(x) function that previously was described in this answer to an SO question on improving calculation speed in C#:

public static d         


        
5条回答
  •  无人及你
    2020-12-04 18:58

    Taylor series approximations (such as the expX() functions in Adriano's answer) are most accurate near zero and can have huge errors at -20 or even -5. If the input has a known range, such as -20 to 0 like the original question, you can use a small look up table and one additional multiply to greatly improve accuracy.

    The trick is to recognize that exp() can be separated into integer and fractional parts. For example:

    exp(-2.345) = exp(-2.0) * exp(-0.345)
    

    The fractional part will always be between -1 and 1, so a Taylor series approximation will be pretty accurate. The integer part has only 21 possible values for exp(-20) to exp(0), so these can be stored in a small look up table.

提交回复
热议问题