Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Peter Pearson Newsgroups: comp.lang.python Subject: Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite Date: 9 Nov 2015 17:51:34 GMT Lines: 28 Message-ID: References: X-Trace: individual.net S1zOs0pJDaS5MVCEW8hu4QxTiOlpVrVnNlVUEWydyXmgvESaJN Cancel-Lock: sha1:foameJ+H7KLg0nfkVZYE6B0nz3o= User-Agent: slrn/pre1.0.0-18 (Linux) Xref: csiph.com comp.lang.python:98546 On Mon, 9 Nov 2015 04:21:14 -0800 (PST), Salvatore DI DIO wrote: > > I was trying to show that this limit was 'e' > But when I try large numbers I get errors > > def lim(p): > return math.pow(1 + 1.0 / p , p) > >>>> lim(500000000) > 2.718281748862504 >>>> lim(900000000) > 2.7182820518605446 !!!! Python floats have close to 16 decimal digits of precision. When you compute 1+1/p with large p, the result will be close to 1, so digits of 1/p beyond the 16th place will be damaged by rounding. For p of 900000000, the first nearly-9 digits of 1/p are zero, so the first "significant" digit is the 10th, and beyond the 16th digit -- the 7th significant digit -- they're damaged, so you can only expect about 16-9 = 7 significant digits to be accurate. And as it turns out, 2.7182820518605446 is good to about 7 significant figures. It takes longer to explain than to see: roundoff limits you to ... (digits of p) + (good digits in the result) = 16 (approximately) -- To email me, substitute nowhere->runbox, invalid->com.