سؤال

i'm trying to write a program that gets from the user a value x and prints sinx using taylor series. but my output is bad. the output i get is not even a number, its -1.#IND00 regardless of what i input.

here's my code

#include <stdio.h>
#include <conio.h>
void main()
{
    int i;
    double x,sum,last;
    sum=(double)0;
    scanf("%f",&x);
    last=x;
    sum=last;
    for(i=1;i<10;i++)
    {
        last*=(double)(-x*x)/((2*i)*(2*i+1));
        sum+=last;
    }
    printf("%f",sum);
    getch();
}
هل كانت مفيدة؟

المحلول

I can see one problem:

scanf("%f",&x);

x is a double, so you need the l, i.e. "%lf".

[true but irrelevant point about how this isn't the right formula for sinh, even though sinh is nowhere mentioned in the question, redacted..]

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top