fmath.c
Go to the documentation of this file.
1 /*
2  * File: fmath.c
3  * Author: Chris Hajduk
4  *
5  * Created on April 12, 2014, 9:53 AM
6  */
7 #include "fmath.h"
8 
10 
12  int i = 0;
13  for (i = 0; i < SINE_TABLE_SIZE; i++){
14  sineTable[i] = sin(((float)i / SINE_TABLE_SIZE) * PI / 2);
15  }
16 }
17 
18 float lookup(int val){
19  int divisor = (65536/SINE_TABLE_SIZE);
20  int index = (val/divisor);
21  float A = sineTable[index & (SINE_TABLE_SIZE - 1)];
22  float B = sineTable[(index + 1) & (SINE_TABLE_SIZE - 1)];
23  float slope = (val & (divisor - 1))/ (float)divisor;
24  return slope * (B - A) + A;
25 }
26 
27 float fSin(float val){
28  if (val >= 0 && val < PI/2){
29  return lookup(val * 65536 / (PI/2));
30  }
31  else if (val >= PI/2 && val < PI){
32  return 1 - lookup((val - PI/2) * 65536 / (PI/2));
33  }
34  else if (val >= PI && val < 3 * PI / 2){
35  return lookup((val - PI) * 65536 / (PI/2)) * -1;
36  }
37  else if (val >= 3 * PI / 2 && val < 2 * PI) {
38  return (1 - lookup((val - 3 * PI / 2) * 65536 / (PI/2))) * -1;
39  }
40  else if (val > 2*PI){
41  return fSin(val - 2 * PI);
42  }
43  else if (val < 0){
44  return fSin(val + 2 * PI);
45  }
46  return 0;
47 }
48 
49 float fCos(float val){
50  return fSin(val + PI/2);
51 }
52 
53 float fTan(float val){
54  return fSin(val)/fCos(val);
55 }
56 
float fCos(float val)
Definition: fmath.c:49
unsigned int i
float fSin(float val)
Definition: fmath.c:27
void initTrigLookup()
Definition: fmath.c:11
#define SINE_TABLE_SIZE
Definition: fmath.h:17
float lookup(int val)
Definition: fmath.c:18
float fTan(float val)
Definition: fmath.c:53
float sineTable[SINE_TABLE_SIZE]
Definition: fmath.c:9
#define PI
Definition: Common.h:36