I'm trying to approximate hyperbolic sine. I need to do it without using math.h library functions.
[don't provide me full solutions, just some hint, because I need to figure it out by myself]
here's what I did:
given the hyperbolic sine taylor series, I need to compute the factorial of (2*n + 1). to do it, I need to do only this step:
fact *= (2*i +1); // inside a for-loop.
I need to compute the power x^(2*n +1), and I did this way:
double power(double x, unsigned int y) {
double result = 1;
for (unsigned int i = 0; i < y; i++) {
result *= x;
}
return result;
}
now, I have every pieces, the taylor series is implemented as follows:
#include <stdio.h>
double power(double x, unsigned int y) {
double result = 1;
for (unsigned int i = 0; i < y; i++) {
result *= x;
}
return result;
}
double hyp_sin(double x) {
double result = 0;
double fact = 1;
double pow = 0;
for (unsigned int i = 0; i != 21; i++) {
fact *= (2 * i + 1);
pow = power(x, 2 * i + 1);
result += ((1 / fact) * pow);
}
return result;
}
int main(void) {
double result = hyp_sin(89.9878);
printf("%lf", result);
return 0;
}
The result is completely wrong, it should have been 6.028024141598018316924203992363e+38 (with 21 iterations)