Thread: calculating e

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    calculating e

    hello,

    I have to calculate the mathetical e.
    So I have this :
    Code:
     */
    
    
    #include        <stdio.h>
    
    
            int
    main ( int argc, char *argv[] )
    {
            int n, teller ;
            float  uitkomst=0;
            printf ("Enter n : ");
            scanf ("%d", &n);
            printf( "n is : %d ", n);
            for (teller=1; teller <=n; teller++)
                    {
                            uitkomst = 1 / teller;
                            printf (" uitkomst  is : %f \n", uitkomst);
                    }
            printf ("e is ongeveer : %f", uitkomst);
    return 0 ;
    }                               /* ----------  end of function main  ---------- */
    But the rule uitkomst = 1/ teller gives as output always zero and not 1/2 , 1/3

    What am I doing wrong now.

    Roelof

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    You'll find out the hard way that c really sucks when you try to mix integers and floats.

    Try this:


    Code:
    uitkomst = 1.0 / teller;

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    That algorithm does not compute e, don't understand why you think it does
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    I'm hoping he simplified it to demonstrate the issue he was having with mixing floats and ints.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I have simplified things because they don't work.

    The right formula is : 1 + 1/1 + 1/(1*2) + 1/ (1*2*3) + 1/ (1*2*3...*n)

    Roelof

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Integer Division
    Integer Division and Modulus

    Use 1.0 instead of 1 as mentioned in mike65535 post.

    Tim S.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    e = d + 1
    That's how I calculate e.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    This is not the way to calculate e, first of all you will end up with 1/n and you don't need the loop. You must first write a function that calculates the factorial of x " fact(x) " and then change:
    uitkomst = 1 / teller;
    to:
    uitkomst += 1 / fact(teller);

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by h_dog View Post
    This is not the way to calculate e, first of all you will end up with 1/n and you don't need the loop. You must first write a function that calculates the factorial of x " fact(x) " and then change:
    uitkomst = 1 / teller;
    to:
    uitkomst += 1 / fact(teller);
    This is not the most efficient way; using a loop is better.
    But, yours is a good and easier to understand way.
    But you should use "1.0 /" instead of "1/"

    Tim S.

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think you are better off calculating, or more likely, approximating the value of e from the definition of e: lim((1+1/n)^n) where n->infinity. This way you avoid calculating a ton of factorials for no reason. Once you get to a high enough n where you are starting to get the correct value of e, you should experiment with calculating the difference between f(n+1) - f(n) to see if the difference tends to be 0. (where f(n) = (1 + 1/n)^n )
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Personally, if I had to compute e, I would do this
    Code:
    #include <math.h>
    
    int main()
    {
        double e = exp(1.0);
    }
    The precision is naturally limited to the machine precision of a double. But so is any other iterative approach that makes use of a double.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    Everybody thanks for the help.
    I have to use loops because im now reading the chapter about loops.

    Roelof

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating Pi with GMP
    By blkhockeypro19 in forum C Programming
    Replies: 0
    Last Post: 06-10-2010, 04:30 PM
  2. calculating the mean
    By bartleby84 in forum C Programming
    Replies: 9
    Last Post: 08-27-2007, 11:47 AM
  3. calculating e
    By warg in forum C Programming
    Replies: 14
    Last Post: 01-29-2006, 04:57 PM
  4. Help with calculating
    By Moffia in forum Windows Programming
    Replies: 3
    Last Post: 08-05-2005, 01:21 AM