1 /*
2 Cubic Sine Approximation based upon a modified Taylor Series Expansion
3 Author: Ryan Billing (C) 2010
4 
5 This is unlicensed.  Do whatever you want with but use at your own disgression.
6 The author makes no guarantee of its suitability for any purpose.
7 */
8 
9 
10 #ifndef FSIN_H
11 #define FSIN_H
12 
13 #include <math.h>
14 #include "global.h"
15 
16 //globals
17 static const float p2 = M_PI/2.0f;
18 static const float fact3 = 0.148148148148148f; //can multiply by 1/fact3
19 
20 static inline float
f_sin(float x)21 f_sin(float x)
22 {
23 
24     float y;  //function output
25     float tmp;
26     bool sign;
27     if ((x>D_PI) || (x<-D_PI)) x = fmod(x,D_PI);
28     if (x < 0.0f) x+=D_PI;
29     sign = 0;
30     if(x>M_PI) {
31         x = D_PI - x;
32         sign = 1;
33     }
34 
35     if (x <= p2) y = x - x*x*x*fact3;
36     else {
37         tmp = x - M_PI;
38         y = -tmp + tmp*tmp*tmp*fact3;
39     }
40 
41     if (sign) y = -y;
42 
43     return y;
44 }
45 
46 static inline float
f_cos(float x_)47 f_cos(float x_)
48 {
49     return f_sin(p2 + x_);
50 }
51 
52 #endif
53