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


Groups > comp.lang.python > #98518

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Newsgroups comp.lang.python
Subject Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite
Date Mon, 9 Nov 2015 12:50:34 +0000
Lines 54
Message-ID <mailman.172.1447073456.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 GZkqYLFT5d9Uxysp08WHuA1vl7zwlbmS6lv62iYWJjag==
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'subject: + ': 0.07; 'cc:addr:python-list': 0.09; 'happen?': 0.09; 'def': 0.13; '!!!!': 0.16; 'cc:name:python list': 0.16; 'dio': 0.16; 'limit,': 0.16; 'module:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:d 110': 0.16; 'subject:when': 0.16; 'symbol,': 0.16; 'wrote:': 0.16; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; "aren't": 0.22; 'trying': 0.22; 'errors': 0.23; 'performing': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'install': 0.25; 'error': 0.27; 'message-id:@mail.gmail.com': 0.27; 'turns': 0.27; 'decimal': 0.29; 'computing': 0.32; 'point': 0.33; 'raising': 0.33; 'gets': 0.35; 'received:google.com': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'doing': 0.38; 'difference': 0.38; 'received:209': 0.38; 'wrong': 0.38; 'represent': 0.38; 'why': 0.39; 'does': 0.39; 'enough': 0.39; 'show': 0.62; 'more': 0.63; 'between': 0.65; 'limit': 0.65; 'results.': 0.67; 'power': 0.72; '100': 0.79; 'amplified': 0.84; 'oscar': 0.84; 'salvatore': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=THa3XDWqoelxy9zy4e6QvjQW0nxHggMejS7X0XAqTz4=; b=FB5yc5AdpgjRKlDG+/KoJY4kA7/0OABSTmAECamA+eLKpg4PuhZZuQra/KA16YxaSF 2F74qHvgBc7KMSzcPrAyxpsPAgbpiEIQhNw9sSPInW3PbAzUpPNixKFGuIVg8Y04pjnb HxjZqS96boj0U5miedtr/ERKa+yMLT6vvj5i0f5udLooLXAX8wwx4f4utAbangyAjyUt dJY+ts6tzrcizXHg8uXcYav8OvVFExb+KBfgQoG7y+8yIL50iURKP6uyrZ1PDSOVaTrU 3X+kHUcZxCO30ciYuLEwOEWreERZweAsaYK/tNYGeTZqe6vYWxCbH4ZmPTaHAoj+70uM 6ocQ==
X-Received by 10.112.54.169 with SMTP id k9mr13742962lbp.95.1447073454405; Mon, 09 Nov 2015 04:50:54 -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:98518

Show key headers only | View raw


On 9 November 2015 at 12:21, 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 ?

You're performing a floating point calculation and expecting exact results.

Try this:

    >>> lim(10 ** 17)
    1.0

Why does this happen? Well in this case that number is 10**17 and it
turns out that

    >>> 1 + 1.0 / 10**17
    1.0

This is because there aren't enough digits in double precision
floating point to represent the difference between 1 and 1+1e-17. As p
gets larger the addition 1+1.0/p because less and less accurate. The
error in computing that is amplified by raising to a large power p.

You can use more digits by using the decimal module:

    >>> from decimal import Decimal, localcontext
    >>> def lim(p):
    ...     return (1 + 1 / Decimal(p)) ** p
    ...
    >>> with localcontext() as ctx:
    ...     ctx.prec = 100
    ...     lim(10**17)
    ...
    Decimal('2.718281828459045221768878329057436445543726874642885850945607978722364313911964199165598158907225076')

You can also install sympy and find this result symbolically:

    >>> from sympy import Symbol, limit, oo
    >>> p = Symbol('p', integer=True)
    >>> limit((1 + 1/p)**p, p, oo)
    E

--
Oscar

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