Thread: Calculating number e

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    30

    Calculating number e

    Hello,

    I am trying to calculate number "e", up to a given precision, but I face with infinite loop while using "for-loop". I couldnt understand it.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main (void) {
        
        int acc,counter;
        float sum=1 , fake;
        
        printf("Up to which term you want to get closer to #e?\n");
        scanf("%d",&acc);                 // obtain data about precision.
        
        
        
        if(acc<2)    {                    //if accuracy is 1, e is directly 1.
            printf("#e is close to 1");
        }
        
            
        else {                            //if accuracy is bigger than 1, run here
            
            
            
            for(counter=1; counter<acc; counter++ ) {
                 
                fake=1;                //initialize value of "fake" every time.
                
                while (counter>0) {  //find factorial terms
                    
                    fake*=counter;
                    counter--;
                }
                
                sum+= (1/fake);    //add 1/factorial to sum
                
                
            }
            
            printf("you accurate term is %.2f", sum);
        
        }
        
        return 0;
        
        
    }

  2. #2
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    You mistake is in the while loop. It always thinks it's a positive number so it decreases by 1. Then the for loop increases by one so it never gets anywhere. I don't know what fake* is so I can't comment on that.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,181
    Quote Originally Posted by Tien Nguyen
    I don't know what fake* is so I can't comment on that.
    It is the result of arguably poor formatting, i.e., this:
    Code:
    fake*=counter;
    would be somewhat more readable as:
    Code:
    fake *= counter;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Quote Originally Posted by laserlight View Post
    It is the result of arguably poor formatting, i.e., this:
    Code:
    fake*=counter;
    would be somewhat more readable as:
    Code:
    fake *= counter;
    I see. It is implying multiply from my vague memory. Thank you. Good thing I still have the user's guide and reference book to look it up if I ever need more information.

  5. #5
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    I do not know what you are trying to accomplish, but from looking carefully at your program there seems a slight change should do the trick. Let me know if the result is what is right. You might not get the precision you need if acc is just an integer instead of a float. I had to adjust with a type cast to float in one of the calculations.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
         
        int acc, counter, temp;
    
        float sum=1, fake;
         
        printf("Up to which term you want to get closer to #e?\n");
    
        scanf("%d",&acc);                 /* obtain data about precision. */     
         
         
        if(acc<2)                         /*if accuracy is 1, e is directly 1. */
        {
        printf("#e is close to 1");
            }
         else
        {                             /*if accuracy is bigger than 1, run here */
            for(counter=1; counter<acc; counter++ )
        {
                  
            fake=1;                       /*initialize value of "fake" every time. */
            temp = counter;     
            while (counter>0)
            {                     /*find factorial terms*/                 
                    fake*=(float)counter;
                    counter--;
                    }
            counter = temp;     
            sum+= (1/fake);               /*add 1/factorial to sum*/
            }
             
            printf("you accurate term is %.2f", sum);
            }
         
    return(0);
    }

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,646
    Somewhat cleaned up example. Calculating from term to 1 is a bit more accurate as it reduces truncation. An input of 17 terms is all that is needed.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void) {
        int term, counter, temp;
        double sum = 0., fact;
        
        printf("Up to which term you want to calculate e : ");
        scanf("%d",&term);                  /* prompt for number of terms */
    
        for(counter = term; counter > 0; counter-- ) {
            fact = 1.;                      /* generate factorial */
            for(temp = counter; temp > 1; temp--)
                fact *= temp;
            sum += (1./ fact);              /* add 1/factorial to sum */
        }
            
        printf("e is approximately %17.15lf\n", sum);
    
        return 0;
    }

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Isn't e supposed to be 2.718 ... ?

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Tien Nguyen View Post
    Good thing I still have the user's guide and reference book to look it up if I ever need more information.
    Mathematical operators are a very basic component of the C language. It is best to commit all language features to memory, so that reference books are unnecessary. You should never need to look up a reference for what a multiplication-assignment operator does.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,181
    Quote Originally Posted by whiteflags
    Isn't e supposed to be 2.718 ... ?
    Yeah, looks like the first term of 1 is missing, but that is easily fixed by starting sum from 1 instead of 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,646
    That should have been counter >= 0, not counter > 0. That was the last fix I made, but apparently I copied and pasted the version prior to this fix. Corrected version (with the intent to sum 1/n! + ... + 1/1! + 1/0!):

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void) {
        int term, counter, temp;
        double sum = 0., fact;
        
        printf("Up to which term you want to calculate e : ");
        scanf("%d",&term);                  /* prompt for number of terms */
    
        for(counter = term; counter >= 0; counter--) {
            fact = 1.;                      /* generate factorial */
            for(temp = counter; temp > 1; temp--)
                fact *= temp;
            sum += (1./ fact);              /* add 1/factorial to sum */
        }
            
        printf("e is approximately %17.15lf\n", sum);
    
        return 0;
    }
    Last edited by rcgldr; 06-28-2016 at 05:40 PM.

  11. #11
    Registered User
    Join Date
    Nov 2015
    Posts
    30
    Thank you all for your helpful answers. They all helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating the sine of a number from the Taylor sum
    By meadmead in forum C Programming
    Replies: 8
    Last Post: 10-31-2011, 06:02 PM
  2. Need help calculating number of spaces with tabs
    By Programmer_P in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2011, 05:04 PM
  3. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  4. Replies: 8
    Last Post: 02-23-2002, 09:50 PM
  5. Calculating a BIG number
    By Robert_Ingleby in forum C Programming
    Replies: 19
    Last Post: 01-10-2002, 08:09 PM

Tags for this Thread