diff --git a/src/math/p_exp.c b/src/math/p_exp.c index 64eecf2..7f37671 100644 --- a/src/math/p_exp.c +++ b/src/math/p_exp.c @@ -1,10 +1,12 @@ #include - /** * * Calculate exponent (e^a), where e is the base of the natural logarithm * (2.71828.) * + * Based of the algorithm in this paper: http://www.schraudolph.org/pubs/Schraudolph99.pdf, + * It calculates a approximation of e^a very efficiently, but at the cost of accuracy. + * * @param a Pointer to input vector * * @param c Pointer to output vector @@ -14,12 +16,19 @@ * @return None * */ -#include + void p_exp_f32(const float *a, float *c, int n) { - int i; for (i = 0; i < n; i++) { - *(c + i) = expf(*(a + i)); + union + { + float f; + uint32_t i; + } u; + + // = 2^23 / M_LN2 * (*(a+i)) + (127 * 2^23 - 100000) + u.i = 12102203.16156 * ( *( a + i )) + (1065353216 - 100000); + *(c + i) = u.f; } }