GTyron
April 28th, 2009, 10:38 PM
I've been having trouble with an assignment to compute the sine of an angle. We have to write our own function rather than use the one in the standard math library. For the most part I have it but, it seems I am having some trouble understanding the formula given:
Sin( x ) = x – x3/3! + x5/5! – x7/7!
My code:
double sin(float angle) {
int add = 1;
long double radian = angle * PI / 180.0;
long double previousX;
long double x = radian;
long long i = 3;
long double diff;
do {
previousX = x;
//NARROWED PROBLEM DOWN TO HERE
if (add = !add) {
x =+ (power(radian, i) / i);
} else {
x =- (power(radian, i) / i);
}
//END PROBLEM SECTION
i += 2;
if (x > previousX) {
diff = x - previousX;
} else {
diff = previousX - x;
}
} while (diff > 0.00000001);
x = radian - (power(radian, i) / i);
return x;
}
Sin( x ) = x – x3/3! + x5/5! – x7/7!
My code:
double sin(float angle) {
int add = 1;
long double radian = angle * PI / 180.0;
long double previousX;
long double x = radian;
long long i = 3;
long double diff;
do {
previousX = x;
//NARROWED PROBLEM DOWN TO HERE
if (add = !add) {
x =+ (power(radian, i) / i);
} else {
x =- (power(radian, i) / i);
}
//END PROBLEM SECTION
i += 2;
if (x > previousX) {
diff = x - previousX;
} else {
diff = previousX - x;
}
} while (diff > 0.00000001);
x = radian - (power(radian, i) / i);
return x;
}