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 401,675 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 3,449 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.
Views: 3694 | 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,888
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: 110
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,059
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: 419
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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
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: 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

  #7  
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,059
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: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Estimate value of e

  #8  
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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
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

  #9  
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  
Join Date: Sep 2004
Posts: 6,059
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: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Estimate value of e

  #10  
Oct 2nd, 2004
>explain why I did not recognize the mistake in the first place.
I can understand not knowing that floating-point overflow is undefined, but it should be immediately obvious that a double couldn't possibly hold a factorial anywhere close to 3999!.

>but code snippets should be seen as starting points or suggestions, not finished product.
I agree. Unfortunately, those that we help aren't quite so forgiving. They have a tendency to see code we post as set in stone answers that "must" be correct. As such, rather than end up teaching bad habits, we should try to be as accurate and correct as possible.

>None of the snippets or suggestions I make are ones I've built and tested
That's your choice. I prefer to only post code that I've made sure works as expected. However, you should mention that your code wasn't tested and you only "hope" that it works.

>only ideas to get the thread-opener's juices flowing.
That's fine, I do it myself. But I make sure that the example is still correct so that the juices aren't sour.

>Some of my posts have been nit picked for syntax or stuff that misses the bigger picture
That's a rather narrow view I think. It seems to me that it was you who missed the bigger picture. If you don't want to be nitpicked on syntax then give pseudocode. It still conveys the idea and avoids people like me who think that teachers should make an attempt at being correct because they're viewed as role models.

>but please try to have a little patience with those of us who can't devote as much time to posts as you.
I have very little time to devote to posts despite what you may think, but I still manage to check my answers. Thus I have very little patience for people who are either too lazy or too arrogant to make sure that they're right.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Reply

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

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

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

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