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
This is a sinus implementation that should be quite fast, it works like this:
it has an arithemtical implementation of square rooting complex numbers
from analitical math with complex numbers you know that the angle is halfed when a complex number is square rooted
You can take a complex number whose angle you already know (e.g. i, has angle 90 degrees or PI / 2 radians)
Than by square rooting it you can get complex numbers of form cos (90 / 2^n) + i sin (90 / 2^n)
from analitical math with complex numbers you know that when two numbers multiply their angles add up
you can show the number k (one you get as an argument in sin or cos) as sum of angles 90 / 2^n and then get the resulting values by multiplying those complex numbers you precomputed
result will be in form cos k + i sin k
#define PI 3.14159265
#define complex pair
/* this is square root function, uses binary search and halves mantisa */
float sqrt(float a) {
float b = a;
int *x = (int*) (&b); // here I get integer pointer to float b which allows me to directly change bits from float reperesentation
int c = ((*x >> 23) & 255) - 127; // here I get mantisa, that is exponent of 2 (floats are like scientific notation 1.111010101... * 2^n)
if(c < 0) c = -((-c) >> 1); // ---
// |--> This is for halfing the mantisa
else c >>= 1; // ---
*x &= ~(255 << 23); // here space reserved for mantisa is filled with 0s
*x |= (c + 127) << 23; // here new mantisa is put in place
for(int i = 0; i < 5; i++) b = (b + a / b) / 2; // here normal square root approximation runs 5 times (I assume even 2 or 3 would be enough)
return b;
}
/* this is a square root for complex numbers (I derived it in paper), you'll need it later */
complex croot(complex x) {
float c = x.first, d = x.second;
return make_pair(sqrt((c + sqrt(c * c + d * d)) / 2), sqrt((-c + sqrt(c * c + d * d)) / 2) * (d < 0 ? -1 : 1));
}
/* this is for multiplying complex numbers, you'll also need it later */
complex mul(complex x, complex y) {
float a = x.first, b = x.second, c = y.first, d = y.second;
return make_pair(a * c - b * d, a * d + b * c);
}
/* this function calculates both sinus and cosinus */
complex roots[24];
float angles[24];
void init() {
complex c = make_pair(-1, 0); // first number is going to be -1
float alpha = PI; // angle of -1 is PI
for(int i = 0; i < 24; i++) {
roots[i] = c; // save current c
angles[i] = alpha; // save current angle
c = croot(c); // root c
alpha *= 0.5; // halve alpha
}
}
complex cosin(float k) {
complex r = make_pair(1, 0); // at start 1
for(int i = 0; i < 24; i++) {
if(k >= angles[i]) { // if current k is bigger than angle of c
k -= angles[i]; // reduce k by that number
r = mul(r, roots[i]); // multiply the result by c
}
}
return r; // here you'll have a complex number equal to cos k + i sin k.
}
float sin(float k) {
return cosin(k).second;
}
float cos(float k) {
return cosin(k).first;
}
Now if you still find it slow you can reduce number of iterations in function cosin (note that the precision will be reduced)