User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 375,199 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,065 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 3598 | Replies: 20
Reply
Join Date: Sep 2004
Posts: 11
Reputation: ellas747 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ellas747 ellas747 is offline Offline
Newbie Poster

Estimate value of e

  #1  
Sep 29th, 2004
I'm having problem writing code for a program that estimates the value of the mathematical constant e by using this formula e= 1 + 1/1! + 1/2!...
I just cant get this to work. Can someone please help
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,873
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 108
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Estimate value of e

  #2  
Sep 29th, 2004
Hmm ... start by writing a recurisve function to find the factorial of a number. There's a tutorial on recursion in either the C++ or the Computer Science section of this site - I forget which. You should also be able to find this function by doing a quick google search. Then just add 1/ to your factorial equation and it should do the trick
Reply With Quote  
Join Date: Sep 2004
Posts: 11
Reputation: ellas747 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ellas747 ellas747 is offline Offline
Newbie Poster

Re: Estimate value of e

  #3  
Sep 29th, 2004
i guess it worked. I'm getting 0 as the answer. who knows.
Reply With Quote  
Join Date: Sep 2004
Posts: 11
Reputation: ellas747 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ellas747 ellas747 is offline Offline
Newbie Poster

Re: Estimate value of e

  #4  
Sep 30th, 2004
how do you get to calculae a power. is it pow(...)
Reply With Quote  
Join Date: Sep 2004
Posts: 6,009
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 413
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Estimate value of e

  #5  
Sep 30th, 2004
>how do you get to calculae a power. is it pow(...)
Yes, that's one way.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Oct 2004
Location: Austria
Posts: 29
Reputation: ZuK is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ZuK ZuK is offline Offline
Light Poster

Re: Estimate value of e

  #6  
Oct 2nd, 2004
I know that I'm breaking the rule not to make anybody's homwork.
But since the answer is so simple I can not help it to post a solution
#include <iostream>

double fakt(int i)
{ 
  if ( i == 1 )
     return 1.0;
  return fakt( i -1 ) * i;
}

int main() {
   double e = 1.0;
   for ( int i = 1; i < 4000; i++ ) {
      e += 1.0 / fakt(i);
   }
   std::cout << " e = " << e << std::endl;
   return 0;
}
K.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,009
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 413
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Estimate value of e

  #7  
Oct 2nd, 2004
>for ( int i = 1; i < 4000; i++ ) {
>e += 1.0 / fakt(i);
Wow, I'm impressed if you can fit up to 3999! into a double. I just get undefined behavior when the limit of double is exceeded. :rolleyes:
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Oct 2004
Location: Austria
Posts: 29
Reputation: ZuK is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ZuK ZuK is offline Offline
Light Poster

Re: Estimate value of e

  #8  
Oct 2nd, 2004
@ Narue: you are right that 170! is the largest nr. that fits into a double.
but with my compiler the result is still correct because fact() simply returns inf for larger numbers then 170 and 1/inf returns 0.0 (btw I'm using gcc )
K.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,009
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 413
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Estimate value of e

  #9  
Oct 2nd, 2004
>but with my compiler the result is still correct
That's nice, but your code is still wrong even if the compiler that you use happens to interpret "undefined" as something intelligent.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Oct 2004
Location: Austria
Posts: 29
Reputation: ZuK is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ZuK ZuK is offline Offline
Light Poster

Re: Estimate value of e

  #10  
Oct 2nd, 2004
Originally Posted by Narue
>but with my compiler the result is still correct
That's nice, but your code is still wrong even if the compiler that you use happens to interpret "undefined" as something intelligent.
I did not mean to insist on my code being correct. My statement was meant to explain why I did not recognize the mistake in the first place.
K.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 2:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC