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

前端 未结 5 2085
难免孤独
难免孤独 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:56

    Try following alternatives (exp1 is faster, exp7 is more precise).

    Code

    public static double exp1(double x) { 
        return (6+x*(6+x*(3+x)))*0.16666666f; 
    }
    
    public static double exp2(double x) {
        return (24+x*(24+x*(12+x*(4+x))))*0.041666666f;
    }
    
    public static double exp3(double x) {
        return (120+x*(120+x*(60+x*(20+x*(5+x)))))*0.0083333333f;
    }
    
    public static double exp4(double x) {
        return 720+x*(720+x*(360+x*(120+x*(30+x*(6+x))))))*0.0013888888f;
    }
    
    public static double exp5(double x) {
        return (5040+x*(5040+x*(2520+x*(840+x*(210+x*(42+x*(7+x)))))))*0.00019841269f;
    }
    
    public static double exp6(double x) {
        return (40320+x*(40320+x*(20160+x*(6720+x*(1680+x*(336+x*(56+x*(8+x))))))))*2.4801587301e-5;
    }
    
    public static double exp7(double x) {
      return (362880+x*(362880+x*(181440+x*(60480+x*(15120+x*(3024+x*(504+x*(72+x*(9+x)))))))))*2.75573192e-6;
    }
    

    Precision

    Function     Error in [-1...1]              Error in [3.14...3.14]
    
    exp1         0.05           1.8%            8.8742         38.40%
    exp2         0.01           0.36%           4.8237         20.80%
    exp3         0.0016152      0.59%           2.28            9.80%
    exp4         0.0002263      0.0083%         0.9488          4.10%
    exp5         0.0000279      0.001%          0.3516          1.50%
    exp6         0.0000031      0.00011%        0.1172          0.50%
    exp7         0.0000003      0.000011%       0.0355          0.15%
    

    Credits
    These implementations of exp() have been calculated by "scoofy" using Taylor series from a tanh() implementation of "fuzzpilz" (whoever they are, I just had these references on my code).

提交回复
热议问题