#include <stdio.h>
#include <math.h>
#define pi 3.1415926

#include "iatan2_256.c"

// test the fast integer approximation to atan2(x,y) for 6809 - no divides used.
// x,y in range -128:127

static inline int iabs(int i) {
  if (i < 0) return -i;
  return i;
}

int main (int argc, char **argv) {
  int x, y;

  for (x = -128; x < 128; x++) {
    for (y = -128; y < 128; y++) {
      // we allow difference of 1 from atan2 result due to difference in rounding - I think ours is more accurate - in any case, accurate enough for a video game...
      if ((char)iabs(iatan2_256 (x, y) - (char)(255&(int)round((atan2(x,y) * 256) / (2.0 * pi))))>1)
        fprintf (stdout, "x%d y%d iatan2:%d atan2: %3.2f\n", x, y, iatan2_256 (x, y), (atan2(x,y) * 256) / (2.0 * pi) + (atan2(x,y)<0 ? 256:0));
    }
  }

}
