Hi,

The comment for fast_log is IMHO incorrect.

/**
* This function is about 3 times faster than the system log() function
* and has an error of about 0.01%
*/

The relative error can be larger than 7% it is almost nowhere about
0.01% when used in the range [ 0.5 - 1[. Try yourself:

Olaf
#include <math.h>
#include <stdio.h>

float fast_log2 (float val)
{
    union {
        float f;
        int i;
    } v;
    v.f = val;
    const int log_2 = ((v.i >> 23) & 255) - 128;
    v.i &= ~(255 << 23);
    v.i += 127 << 23;

    v.f = ((-1.0f/3) * v.f + 2) * v.f - 2.0f/3;   // (1)

    return (v.f + log_2);
}

#ifndef log2
float log2 (float val)
{
  return log( val)/ log(2.f);
}
#endif

int main() {
  float x = 0.995;

  printf("x\tabs. error\trel. error\n");
  printf("%g\t%g\t%g\n", x, fast_log2(x)-log2(x ), (fast_log2(x)-log2(x 
))/log2(x) );
}
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to