Thread: Cos func.

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97

    Cos func.

    Okay so this is for homework and i am stuck, we have to write our own cos func. I understand how to set every thing up and i was given the formula that

    cos(x)=(-1^n)*((x^2n)/(2n)!)

    this is what i have set up so far. any help would be great.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <math.h>
    
    float cosd(float);
    
    void main() // shell to test the cosd() function
    {            // This program is a poor example of programming.
                // It is only for testing another function, in
                // this case the cosd() function.
    
        float angle, answer;
    
        do
        {
            printf("Enter angle (in degrees): ");
            scanf("%f",&angle);
            answer=cosd(angle);
            printf("cos(%2f)=%6.4f\n\n",angle,answer);
        } while (angle != -99.0); // -99 is sentinel value
    }
    // cosd() function goes here
     float cosd(float d)
     {
         float r,term,sum,result;
         r=(d)*(3.14159265/180);
         term=1;
         while(term > 0.0005)
         {
             sum=((r*r)/((term*2)*(2*term-1)))*(term-1);
         }
         return sum;    
     }
    I mean i got the function to work by doing

    Code:
     float cosd(float d)
     {
         float r,sum;
         r=(d)*(3.14159265/180);
         sum= 1-((pow(r,2)/2))+(pow(r,4)/(4*3*2))-(pow(r,6)/(6*5*4*3*2))+(pow(r,8)/(8*7*6*5*4*3*2))-(pow(r,10)/(10*9*8*7*6*5*4*3*2))+(pow(r,12)/(12*11*10*9*8*7*6*5*4*3*2))-(pow(r,14)/(14*13*12*11*10*9*8*7*6*5*4*3*2));
         return sum;    
     }
    but my professor wants an algorithm that generates a term based on the previous term and i just don't see how to do it, yet.
    Last edited by omGeeK; 10-15-2010 at 07:47 PM.

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by omGeeK View Post
    Okay so this is for homework and i am stuck, we have to write our own cos func. I understand how to set every thing up and i have the formula that

    cos(x)=(-1^n)*((x^2n)/(2n)!)

    this is what i have set up so far. any help would be great.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <math.h>
    
    float cosd(float);
    
    void main() // shell to test the cosd() function
    {            // This program is a poor example of programming.
                // It is only for testing another function, in
                // this case the cosd() function.
    
        float angle, answer;
    
        do
        {
            printf("Enter angle (in degrees): ");
            scanf("%f",&angle);
            answer=cosd(angle);
            printf("cos(%2f)=%6.4f\n\n",angle,answer);
        } while (angle != -99.0); // -99 is sentinel value
    }
    // cosd() function goes here
     float cosd(float d)
     {
         float r,term,sum,result; <--- result is not being used
         r=(d)*(3.14159265/180);
         term=1;
         while(term > 0.0005)
         {
             sum=((r*r)/((term*2)*(2*term-1)))*(term-1);
         }
         return sum;    
     }
    For projects like this, I'd just go ahead and use doubles. In my case, my math function
    expects doubles anyway.

    In red above, I don't see the variable term being changed, hence it's locked in
    a forever loop.

    I haven't checked the rest of your code.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    yea i understand why i should be using doubles, but its what my professor wanted . And i understand that its not changing its just that to be honest we were given some pointers and term was one of them and i dont really understand it. I mean i got the function to work by doing

    Code:
     float cosd(float d)
     {
         float r,sum;
         r=(d)*(3.14159265/180);
         sum= 1-((pow(r,2)/2))+(pow(r,4)/(4*3*2))-(pow(r,6)/(6*5*4*3*2))+(pow(r,8)/(8*7*6*5*4*3*2))-(pow(r,10)/(10*9*8*7*6*5*4*3*2))+(pow(r,12)/(12*11*10*9*8*7*6*5*4*3*2))-(pow(r,14)/(14*13*12*11*10*9*8*7*6*5*4*3*2));
         return sum;    
     }
    but my professor wants an algorithm that generates a term based on the previous term and i just don't see how to do it, yet.
    Last edited by omGeeK; 10-15-2010 at 07:47 PM.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    i think if i use a while loop and use the equation x^2/2! somehow it should work, any thoughts anyone?

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Can you please help me understand this? I have never seen this before:

    cos(x)=(-1^n)*((x^2n)/(2n)!)

    Do you mean

    cos(x)=[Summation from i = 0 to n] (-1^i)*((x^2i)/(2i)!)

    Or is it a recurrence relation? Please state it a little more precisely.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Do you mean he wants you to write your own factorial function maybe? A simple one might be:
    Code:
    int factorial(int num)
    {
      int rv = 1;
      while(num > 1)
        rv *= num--;
      return rv;
    }
    Then you could just do factorial(5) instead of (5*4*3*2).
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by itsme86 View Post
    Do you mean he wants you to write your own factorial function maybe?
    I am pretty certain that the subject of this thread is the cosine function, not the factorial function. I could be wrong however.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Yea i basically am rewriting the cos func with my own code.
    Last edited by omGeeK; 10-16-2010 at 05:27 PM.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Quote Originally Posted by QuadraticFighte View Post
    Can you please help me understand this? I have never seen this before:

    cos(x)=(-1^n)*((x^2n)/(2n)!)

    Do you mean

    cos(x)=[Summation from i = 0 to n] (-1^i)*((x^2i)/(2i)!)

    Or is it a recurrence relation? Please state it a little more precisely.
    Well the cosine function is an infinite function so i would think it is recurring.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by omGeeK View Post
    Well the cosine function is an infinite function so i would think it is recurring.
    Or an infinite summation. I am just wondering what it is you posted.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    well the cosine of an angle is 1-(x^2/2!)+(x^4/4!)-(x^6/6!)+(x^8/8!)................ going on forever but i only need the accuracy of 7 terms.

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by QuadraticFighte View Post
    I am pretty certain that the subject of this thread is the cosine function, not the factorial function. I could be wrong however.
    The reason he might have pointed out is because the formula requires to find the factorial

    Code:
    cos(x)=[Summation from i = 0 to n] (-1^i)*((x^2i)/(2i)!)
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by omGeeK View Post
    well the cosine of an angle is 1-(x^2/2!)+(x^4/4!)-(x^6/6!)+(x^8/8!)................ going on forever but i only need the accuracy of 7 terms.
    I know that, but you said

    Quote Originally Posted by omGeeK
    but my professor wants an algorithm that generates a term based on the previous term and i just don't see how to do it, yet.
    ...leading me to think your professor wants you to code up a recurrence relation. I know of no such relation.

  14. #14
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    haha well the homework page states that we should generate a term from a previous term and it gives us the formula which i gave earlier
    1-(x^2/2!)+(x^4/4!)-(x^6/6!)+(x^8/8!)...
    this is what i was given and i am having a hard time understanding it.
    Last edited by omGeeK; 10-16-2010 at 06:15 PM.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Code:
    sum= 1-((pow(r,2)/2))+(pow(r,4)/(4*3*2))-(pow(r,6)/(6*5*4*3*2))+(pow(r,8)/(8*7*6*5*4*3*2))-(pow(r,10)/(10*9*8*7*6*5*4*3*2))+(pow(r,12)/(12*11*10*9*8*7*6*5*4*3*2))-(pow(r,14)/(14*13*12*11*10*9*8*7*6*5*4*3*2));
    since the above worked correctly, all you need to do is write a recursive function that computes the cos to the needed accuracy i.e. 7
    along with that you need to write a factorial function.
    Code:
    int num=1;
    int accuracy=7;//changing to even may mess up recursion
    double sum=0;
    while(accuracy!=-1)//need the extra loop to count the 1
    {
    sum+=pow(-1,(2*(accuracy%2)))*(pow(r,num)/fact(num));//fact is the factorial function
    if(num==1){sum=1;}//another quick fix, too lazy to count :P
    accuracy--;
    num+=2;
    if(num==3){num--;}//quick fix
    }
    this is untested code, and i myself am just a first year cs student, sooo... mistakes are very probable))
    also to the purists out there: i know the if checks are bad coding style, but for demo purposes everything goes a'ight?)
    P.S. first post
    Last edited by tty0; 10-16-2010 at 06:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with cos, sin and tan^-1 (arctan);
    By xIcyx in forum C Programming
    Replies: 15
    Last Post: 04-23-2007, 09:14 AM
  2. sin() and cos() that use degrees
    By dwks in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 04:29 PM
  3. cos() returning wrong result?
    By rmullen3 in forum C++ Programming
    Replies: 6
    Last Post: 08-20-2002, 11:46 PM
  4. typedef a func
    By trekker in forum C Programming
    Replies: 4
    Last Post: 07-02-2002, 05:15 AM
  5. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM