Thread: cosine series with out using math.h

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    cosine series with out using math.h

    I want to write a pogram which will calculate cosine series.
    But I am not get the value like cos() function of math.h
    Could you tell me why it is happening and any error in my code or any step mistake ?????

    Code:
    float cos1(float x)
          {
          int i,fact=1;
          float sum,term;
          sum=1.0;
          term=1.0;
          for(i=1;i<5;i++)
                           {
                            term=term*x*x;
                            term=term*(-1);
                            fact=factorial(2*i);
                            sum=sum+(term/fact);    
                            }
           return(sum);                      
          }
    
    int factorial(int x)
        {
        int fact;
        if(x==1)
                return(1);
        else
                fact=x*factorial(x-1);       
        return(fact);                
        }               
    
    also in the place of for loop i am using  do while loop but result is same 
    
    float cos1(float x)
          {
          int i,fact=1;
          float sum,term;
          sum=1.0;
          term=1.0;
          i=1;
          do
                           {
                            term=term*x*x;
                            term=term*(-1);
                            fact=factorial(2*i);
                            sum=sum+(term/fact);    
                            i++;
                            }
           while(term>0.00001)
            return(sum);                      
          }

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    41
    I copied and pasted your code and compiled it and it appears to work fine. It gives the same values as my calculator.

    What kind of values does it give you?

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    And just what exactly does that have to do with C#?
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Moved to C
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                            term=term*x*x;
                            term=term*(-1);
    can be written simpler as:
    Code:
    term *= -x * x;
    and it's likely that the compiler generates better code this way too - multiplying by -1 is an expensive way to negate a number, when you can do "x = -x" if you want to negate it - and if the compiler doesn't have specific code to identify the "multiply x by -1 is the same as -x", then it will do a much worse job of it.

    You can also calculate the factorial iteratively within the loop itself - just multiply by i * 2-1 and i * 2 on to a starting value of 1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cosine - Maclaurin Series
    By carrotcake1029 in forum C Programming
    Replies: 12
    Last Post: 12-07-2008, 12:20 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  4. Problem with implementing Liebniz series
    By C_Necessity in forum C Programming
    Replies: 6
    Last Post: 06-15-2005, 12:39 PM
  5. taylor series using only stdio.h
    By faruque in forum C Programming
    Replies: 1
    Last Post: 02-13-2003, 12:50 PM