I am googling the question for past hour, but there are only points to Taylor Series or some sample code that is either too slow or does not compile at all. Well, most answe
Sharing my code, it's a 6th degree polynomial, nothing special but rearranged to avoid pows. On Core i7 this is 2.3 times slower than standard implementation, although a bit faster for [0..2*PI] range. For an old processor this could be an alternative to standard sin/cos.
/*
On [-1000..+1000] range with 0.001 step average error is: +/- 0.000011, max error: +/- 0.000060
On [-100..+100] range with 0.001 step average error is: +/- 0.000009, max error: +/- 0.000034
On [-10..+10] range with 0.001 step average error is: +/- 0.000009, max error: +/- 0.000030
Error distribution ensures there's no discontinuity.
*/
const double PI = 3.141592653589793;
const double HALF_PI = 1.570796326794897;
const double DOUBLE_PI = 6.283185307179586;
const double SIN_CURVE_A = 0.0415896;
const double SIN_CURVE_B = 0.00129810625032;
double cos1(double x) {
if (x < 0) {
int q = -x / DOUBLE_PI;
q += 1;
double y = q * DOUBLE_PI;
x = -(x - y);
}
if (x >= DOUBLE_PI) {
int q = x / DOUBLE_PI;
double y = q * DOUBLE_PI;
x = x - y;
}
int s = 1;
if (x >= PI) {
s = -1;
x -= PI;
}
if (x > HALF_PI) {
x = PI - x;
s = -s;
}
double z = x * x;
double r = z * (z * (SIN_CURVE_A - SIN_CURVE_B * z) - 0.5) + 1.0;
if (r > 1.0) r = r - 2.0;
if (s > 0) return r;
else return -r;
}
double sin1(double x) {
return cos1(x - HALF_PI);
}