[Tutor] value of 'e'

Shashwat Anand anand.shashwat at gmail.com
Tue Nov 24 12:47:06 CET 2009

Followed by this discussion on Hacker
News<http://news.ycombinator.com/item?id=958323>I checked this
link <http://www.isotf.org/?page_value=13223> and was wondering how to
calculate value of 'e' <http://mathworld.wolfram.com/e.html> to a large

as e = 1/0! + 1/1! +1/2! .... and so on...
so i wrote this:
>>> sum(1.0 / math.factorial(i) for i in range(100))

It was not giving the precision that I wanted so I tried decimal module of
which I was not much aware of.

>>> decimal.getcontext().prec = 100
>>> sum(decimal.Decimal(str(1./math.factorial(decimal.Decimal(i)))) for i in

Until now no problem
I matched the value of 'e' from
which claims it have 10 million digits of e
the first few digits of 'e' from there which doesn't match with the result I

so i tried,
>>> sum(decimal.Decimal(str(1./math.factorial(decimal.Decimal(i)))) for i in
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<input>", line 2, in <genexpr>
OverflowError: long int too large to convert to float

And then i went clueless !!
How can it be done ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/6e6cda7e/attachment.htm>

More information about the Tutor mailing list