User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,765 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,498 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 Python advertiser:
Jan 8th, 2008
Views: 1,973
This short Python program calculates Euler's number e, the base of natural logarithms, to a precision of at least 100 digits in this case. To get the precision needed, the Python module decimal is used. The result is compared to a published result.
Last edited : Jan 8th, 2008.
python Syntax
  1. # Euler's number e, the base of natural logs
  2. # e is the sum of this infinite series:
  3. # e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + ...
  4. # or simplified ...
  5. # e = 2 + 1/2! + 1/3! + 1/4! + ...
  6. # to get higher floating point precision use the module decimal
  7. # tested with Python25 vegaseat 08jan2008
  8.  
  9. import decimal as dc
  10.  
  11. # set the precision
  12. dc.getcontext().prec = 101
  13.  
  14. factorial = 1
  15. euler = 2
  16. for x in range(2, 150):
  17. factorial *= x
  18. euler += dc.Decimal(str(1.0))/dc.Decimal(str(factorial))
  19.  
  20. print "Eulers number calculated and 100 digit reference below:"
  21.  
  22. print euler
  23.  
  24. # e from http://www.gutenberg.org/etext/127 (up to 1 million places)
  25. e = "2.7182818284590452353602874713526624977572470936999595749669676277\
  26. 240766303535475945713821785251664274"
  27.  
  28. print e
  29.  
  30. """
  31. my output --->
  32. Eulers number calculated and 100 digit reference below:
  33. 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274
  34. 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274
  35. """
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 4:05 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC