Estimate value of e

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 11
Reputation: ellas747 is an unknown quantity at this point 
Solved Threads: 0
ellas747 ellas747 is offline Offline
Newbie Poster

Estimate value of e

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,047
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Estimate value of e

 
0
  #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
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 11
Reputation: ellas747 is an unknown quantity at this point 
Solved Threads: 0
ellas747 ellas747 is offline Offline
Newbie Poster

Re: Estimate value of e

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

Re: Estimate value of e

 
0
  #4
Sep 30th, 2004
how do you get to calculae a power. is it pow(...)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,779
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Estimate value of e

 
0
  #5
Sep 30th, 2004
>how do you get to calculae a power. is it pow(...)
Yes, that's one way.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: ZuK is an unknown quantity at this point 
Solved Threads: 0
ZuK ZuK is offline Offline
Light Poster

Re: Estimate value of e

 
0
  #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
  1. #include <iostream>
  2.  
  3. double fakt(int i)
  4. {
  5. if ( i == 1 )
  6. return 1.0;
  7. return fakt( i -1 ) * i;
  8. }
  9.  
  10. int main() {
  11. double e = 1.0;
  12. for ( int i = 1; i < 4000; i++ ) {
  13. e += 1.0 / fakt(i);
  14. }
  15. std::cout << " e = " << e << std::endl;
  16. return 0;
  17. }
K.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,779
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Estimate value of e

 
0
  #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:
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: ZuK is an unknown quantity at this point 
Solved Threads: 0
ZuK ZuK is offline Offline
Light Poster

Re: Estimate value of e

 
0
  #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 Quick reply to this message  
Join Date: Sep 2004
Posts: 7,779
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Estimate value of e

 
0
  #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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: ZuK is an unknown quantity at this point 
Solved Threads: 0
ZuK ZuK is offline Offline
Light Poster

Re: Estimate value of e

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC