Saturday, 28 September 2013

Cosine of a number with no math.h. Unknown error 1.#INF00

Cosine of a number with no math.h. Unknown error 1.#INF00

Well, I have a problems with a specific C code. I'm dealing with solving a
task without using any "refined" coding, that is, doing regular stuff with
the simplest of the libraries and functions. Particulary, I am ONLY
allowed to use stdio.h , not any library more. And no strings, also.
The problem: I have to do the cosine of a real number x (stored in
double), which is given by the user, using this expression: cos (x) =
((x^(2i))/((2i)!))*(-1)^i, all this inside a sumatore from i = 0 to i = n,
being n the precision (an int number) which the user chooses. Ok, I've
managed to do this code so far:
scanf("%lf", &x);
r1 = x;
scanf("%d", &n);
for (i = 0; i <= n; i++){
for(cont1 = 0; cont1 < 2*i; cont1++){
r1 = r1*r1;
}
for(cont2 = 1; cont2 <= 2*i; cont2++){
r2 = r2*cont2;
}
for(cont3 = 1; cont3 < i; cont3++){
r3 = (-1)*(-1);
}
result = result + (r1/r2)*r3;
}
printf("The result is %lf\n", result);
But the output is always 1.#INF00, all the time. Does anybody know why
this happens? Could somebody tell me how to manage to get the right code?
It would be very appreciated.
Thank you in advance.

No comments:

Post a Comment