Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98517

Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite
Date Mon, 9 Nov 2015 23:45:01 +1100
Lines 56
Message-ID <mailman.171.1447073105.16136.python-list@python.org> (permalink)
References <ea008366-9aa2-47e6-b4b7-69e5d447d408@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 2+/VhGC2EP0ukPhzWtz0AQ+u0LOKXvLPtxTGLSFOVKLw==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'received:209.85.223': 0.03; 'subject: + ': 0.07; 'cc:addr:python-list': 0.09; 'def': 0.13; '!!!!': 0.16; 'dio': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'precision.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'screen,': 0.16; 'subject:when': 0.16; 'zero,': 0.16; 'wrote:': 0.16; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'trying': 0.22; 'bigger': 0.23; 'errors': 0.23; 'header:In-Reply- To:1': 0.24; 'mon,': 0.24; 'error': 0.27; 'message- id:@mail.gmail.com': 0.27; 'actual': 0.28; 'pile': 0.29; "we're": 0.30; 'push': 0.30; 'skip:d 40': 0.32; 'point': 0.33; 'problem': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'nov': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'starting': 0.37; 'doing': 0.38; 'received:209': 0.38; 'wrong': 0.38; 'rather': 0.39; 'your': 0.60; 'close': 0.61; "you'll": 0.61; 'show': 0.62; 'further': 0.62; 'here.': 0.62; 'more': 0.63; 'limit': 0.65; 'chrisa': 0.84; 'salvatore': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=iBCmMZS/8xqCR6QBNsyVyDOiBjd35PdAI+1ecRih8Nc=; b=M3njTpynrizXSF4Kwrh1rbenu9IDuLbqhnBOUAZA6yDxFiAH6+qabjZ6uBAKszvzUK SdjkomoEmOGVZCpoACOSlA3xNuc590Dh16Fhl+E3vESvoXebTeCCk9jUuazX8+/JQs0U mml9VzOv+BdsQMwkzebyrVQrUzHymjHDi0aju6X9N7qBOS7nU6fm7sWWkqJkVsX04j64 w0R7DqK9Y7Qrc6IIbHV58sRyYvFe7jkjTX+nvSKYG7OZLXI7G4rbaV64IXpTOdvHYx1O fYw/m+jkqCWgbQnU/cnU/2uwI87bhY0ZRZNVSIKYTFeMlKBdVe6mW+b5uilPktHo2SOc XFig==
X-Received by 10.107.10.233 with SMTP id 102mr8108640iok.31.1447073101340; Mon, 09 Nov 2015 04:45:01 -0800 (PST)
In-Reply-To <ea008366-9aa2-47e6-b4b7-69e5d447d408@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98517

Show key headers only | View raw


On Mon, Nov 9, 2015 at 11:21 PM, Salvatore DI DIO <artyprog@gmail.com> 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  !!!!
>
>
> What am i doing wrong ?
>

Floating point error is going to start becoming a major problem here.
Eventually, 1.0/p will underflow to zero, and you'll simply get a
result of 1.0:

>>> lim(900000000000000000000)
1.0

You could try using decimal.Decimal instead; that can give you rather
more precision. Or if you have a lot of patience, fractions.Fraction.

>>> def lim(p):
    return (1 + decimal.Decimal(1) / p) ** p

>>> lim(500000000)
Decimal('2.718281825740763411884758912')
>>> lim(900000000)
Decimal('2.718281826948888665260445479')
>>> lim(900000000000000000000)
Decimal('2.718281556630875980862943027')

Definitely not perfect yet... but let's crank up the precision.

>>> decimal.getcontext().prec=200
>>> lim(500000000)
Decimal('2.7182818257407634118847589119866382434352189174535941028176465917058364010909850212743201574998148959449308935216863472501568773422911827403060031110458945666576132256505421665510943751730347310393611')
>>> lim(900000000)
Decimal('2.7182818269488886655322736616533366198705073189604924113513719666470682138645212144697520852594221964992741852547403862053248620085931699038380869918694830598723560639286551799819233225160142059050076')
>>> lim(900000000000000000000)
Decimal('2.7182818284590452353587773147812963615153818054494196724217932791045775211554493949916225628968841872263472976692247791433837657091684393783360159727396877123886624050885921242672885287748600658242797')

Now we're starting to get pretty close to the actual value of e. You
can push the precision further if you like; it'll take longer to
calculate, and dump a bigger pile of digits onto your screen, but
it'll be more accurate.

I don't recommend using fractions.Fraction unless you have a really
fast computer and a LOT of patience. It'll take three parts of forever
to get a result... but that result _will_ be perfectly accurate :)

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Calulation in  lim  (1  + 1 /n) ^n  when  n -> infinite Salvatore DI DIO <artyprog@gmail.com> - 2015-11-09 04:21 -0800
  Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite Chris Angelico <rosuav@gmail.com> - 2015-11-09 23:45 +1100
    Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite Salvatore DI DIO <artyprog@gmail.com> - 2015-11-09 07:13 -0800
  Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-11-09 12:50 +0000
    Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite Salvatore DI DIO <artyprog@gmail.com> - 2015-11-09 07:13 -0800
    Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite Salvatore DI DIO <artyprog@gmail.com> - 2015-11-09 07:15 -0800
  Re: Calulation in  lim  (1  + 1 /n) ^n  when  n -> infinite Peter Pearson <pkpearson@nowhere.invalid> - 2015-11-09 17:51 +0000
    Re: Calulation in  lim  (1  + 1 /n) ^n  when  n -> infinite Salvatore DI DIO <artyprog@gmail.com> - 2015-11-10 00:26 -0800

csiph-web