Sunday, February 8, 2015

displaying sin, cos and tan series


#include< stdio.h >
#include< math.h >
long double sine(float);
long double cosine(float);
long double tangent(float);
void main()
{
    int count=0;
    float x;
    long double y1,y2,y3,yt;
    clrscr();

    for(x=0;x<=360;x+=5)
    {
        y1=sine(x);
        y2=cosine(x);
        y3=tangent(x);
        if(y3<0 br="">            yt=-y3;
        else
            yt=y3;
        count++;
        printf("sin(%3.0f) = %12.8Lf  cos(%3.0f) = %12.8Lf",x,y1,x,y2);
        if(yt>80)
            printf("  tan(%3.0f) = **undefined**\n",x);
        else
            printf("  tan(%3.0f) = %12.8Lf\n",x,y3);
        if(count%20==0)
        {
            printf("\npress any key to continue...");
            getch();
            clrscr();
        }

    }
    getch();
}
long double sine(float x)
{
    long double csinx,psinx,cps,fact,xp,xrad;
    int n,sf,i;
    xrad=x*3.141592653589793/180;
    csinx=xrad;
    psinx=1;
    cps=csinx-psinx;
    if(cps<0 br="">        cps=-cps;
    sf=-1;
    n=3;
    while(cps>1e-16)
    {
        psinx=csinx;
        xp=1;
        fact=1;
        for(i=1;i<=n;i++)
        {
            xp=xp*xrad;
            fact=fact*i;
        }
        csinx=csinx+sf*xp/fact;
        cps=csinx-psinx;
        if(cps<0 br="">            cps=-cps;
        n=n+i;
        sf=-sf;
    }
    return csinx;
}
long double cosine(float x)
{
    long double ccosx,pcosx,cps,fact,xp,xrad;
    int n,sf,i;
    xrad=x*3.141592653589793/180;
    ccosx=1;
    pcosx=0;
    cps=ccosx-pcosx;
    if(cps<0 br="">        cps=-cps;
    sf=-1;
    n=2;
    while(cps>1e-16)
    {
        pcosx=ccosx;
        xp=1;
        fact=1;
        for(i=1;i<=n;i++)
        {
            xp=xp*xrad;
            fact=fact*i;
        }
        ccosx=ccosx+sf*xp/fact;
        cps=ccosx-pcosx;
        if(cps<0 br="">            cps=-cps;
        n=n+i;
        sf=-sf;
    }
    return ccosx;
}
long double tangent(float x)
{
    long double y;
    y=sine(x)/cosine(x);
    return y;
}

No comments:

Post a Comment