Main Archive Specials Wiki | FAQ Links Submit Forum

 fast exp() approximationsType : Taylor series approximationReferences : Posted by scoofy[AT]inf[DOT]elte[DOT]huNotes : I needed a fast exp() approximation in the -3.14..3.14 range, so I made some approximations based on the tanh() code posted in the archive by Fuzzpilz. Should be pretty straightforward, but someone may find this useful. The increasing numbers in the name of the function means increasing precision. Maximum error in the -1..1 range: fastexp3: 0.05 (1.8%) fastexp4: 0.01 (0.36%) fastexp5: 0.0016152 (0.59%) fastexp6: 0.0002263 (0.0083%) fastexp7: 0.0000279 (0.001%) fastexp8: 0.0000031 (0.00011%) fastexp9: 0.0000003 (0.000011%) Maximum error in the -3.14..3.14 range: fastexp3: 8.8742 (38.4%) fastexp4: 4.8237 (20.8%) fastexp5: 2.28 (9.8%) fastexp6: 0.9488 (4.1%) fastexp7: 0.3516 (1.5%) fastexp8: 0.1172 (0.5%) fastexp9: 0.0355 (0.15%) These were done using the Taylor series, for example I got fastexp4 by using: exp(x) = 1 + x + x^2/2 + x^3/6 + x^4/24 + ... = (24 + 24x + x^2*12 + x^3*4 + x^4) / 24 (using Horner-scheme:) = (24 + x * (24 + x * (12 + x * (4 + x)))) * 0.041666666f Code : inline float fastexp3(float x) { return (6+x*(6+x*(3+x)))*0.16666666f; } inline float fastexp4(float x) { return (24+x*(24+x*(12+x*(4+x))))*0.041666666f; } inline float fastexp5(float x) { return (120+x*(120+x*(60+x*(20+x*(5+x)))))*0.0083333333f; } inline float fastexp6(float x) { return 720+x*(720+x*(360+x*(120+x*(30+x*(6+x))))))*0.0013888888f; } inline float fastexp7(float x) { return (5040+x*(5040+x*(2520+x*(840+x*(210+x*(42+x*(7+x)))))))*0.00019841269f; } inline float fastexp8(float x) { return (40320+x*(40320+x*(20160+x*(6720+x*(1680+x*(336+x*(56+x*(8+x))))))))*2.4801587301e-5; } inline float fastexp9(float x) { return (362880+x*(362880+x*(181440+x*(60480+x*(15120+x*(3024+x*(504+x*(72+x*(9+x)))))))))*2.75573192e-6; }