I am trying to create a calculator for sin and cosine that technically only operates on the range of 0-pi/2. Right now this might seem silly but later on it will be used so I can employ Taylor series.
I have a mostly working implementation however I have a serious issue when theta is in the form of x * (pi/2) where x is an arbitrary integer. It would appear that on these values that sometimes they are pushed into a nearby quadrants they dont belong to. There are also some occasional outright errors that I cannot explain.
How can I shore this up making it more efficient and correct?
Here is the code for doing this.
#define T_PI (2.0 * M_PI)
#define H_PI (0.5 * M_PI)
void sincos(float theta, float* cosine, float* cosine) {
int mode;
prepareForRange(&theta, cosine, sine);
Assert(!(f < 0.0 || f > H_PI));
*cosine = cos(theta);
*sine = sin(theta);
range_output(mode, cosine, sine);
}
void prepareForRange(float* theta, int* mode, float *cosine, float* sine) {
if (*theta < 0.0) *theta += ceil(-*theta / T_PI) * T_PI;
*mode = (int)floor(*theta / H_PI) % 4 + 1;
*theta = fmodf(*theta, H_PI);
}
void range_output(int mode, float *cos, float *sin) {
float temp;
switch (mode) {
case 1:
break;
case 2:
temp = *cos;
*cos = -*sin;
*sin = temp;
break;
case 3:
*cos = -*cos;
*sin = -*sin;
break;
case 4:
temp = *cos;
*cos = *sin;
*sin = -temp;
break;
default:
break;
}
}