Hello

I have a question regarding this program in c.
My assignement is as follows :
*read the angle in degrees
*convert that angle in radians
*Apply the formula for calculating cosine function with factorials

*Then, automate the above calculating procedure by using loops

*Afterwords calculate the absolute error between those two cases

#include<stdio.h>
#include<math.h>
int main(void)
{
    double s_alpha;
    double s_alpha2=0;
    double j;
    short angle,i;
    double rad;
    long q;

    do
    {
        printf("\nInput angle [Degrees] : ");
        scanf("%hd",&angle);

        if(angle>0&&angle<360)
        {
            break;
            }
        printf("\nNot allowed_repeat\n");
    }while(angle<=0||angle>=360);

    rad=angle*(M_PI/180);

    s_alpha=1-((pow(rad,2))/(1*2)+
              (pow(rad,4))/(1*2*3*4)-
              (pow(rad,6))/(1*2*3*4*5*6)+
              (pow(rad,8))/(1*2*3*4*5*6*7*8)-
              (pow(rad,10))/(1*2*3*4*5*6*7*8*9*10)+
              (pow(rad,12))/(1*2*3*4*5*6*7*8*9*10*11*12));   //factorials by 'hand'

    int count=0;
    for(i=2;i<13;i+=2)
    {
        q=1.0;
        j=1.0;

        while(j<=i)            //factorials in a loop
        {
            q=q*j++;

            }

    count++;
        if(count%2==0)
        {
            j=(pow(rad,i)/q);
            s_alpha2-=j;
            }

        else
        {
            j=(pow(rad,i)/q);
            s_alpha2+=j;
            }

    }
    s_alpha2=1-s_alpha2;
    printf("\nAngle of %hd deg. = %g rad\n",angle,rad);
    for(i=0;i<27;i++)
    {
        printf("-");
        }

    printf("\nOutput: \nCalculated via function : \t%lf",s_alpha);
    printf("\nCalculated via loop : \t\t%lf",s_alpha2);
    printf("\nAbs error : \t\t\t%lf",s_alpha2-s_alpha);
    getchar();
    return 0;
}

What I would be much interested in learning is that is it possible for such a large error to appear while comparing those two cases. I also understand that there could also be a defect with the code, so any input is appreciated. Thank you very much !

Recommended Answers

All 4 Replies

What is the range of shorts on your system? My old compiler has a SHRT_MAX in the header file <limits.h>, that won't go to 360.

If you make a habit of using the absolute minimal data type for your variables, you WILL get bitten by the "overflow" bug, a great deal. Engineers never build a structure to withstand a certain stress - it's always a certain stress PLUS a safety margin.

I think they started doing that about the time that architects/builders were killed if their buildings collapsed. ;)

I think they started doing that about the time that architects/builders were killed if their buildings collapsed.

lol.
Yes, well, my short range on a win7 64 [-32768;32767],GCC compiler, CB IDE. Ive started to explore the world of floating point arithmetics, and I understand the fact that both float and double data dont come out the way we would expect[of course if we apply arithmetic operations on them]. But the accuracy comming out from this loop is great. Does the loop affect the calculating accuarcy of the given task?

It looks like you're doing this in the upper code:

j=((pow(rad,i))/q;

but doing this in the lower code:

j=(pow(rad,i)/q);

I'm not sure if they're equivalent, or not.

That was a common punishment for an architect's failure, in the Ancient world. Kept the guys on their toes. ;)

Thanks. The error still exists, but thats probably the way it has to be. Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.