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

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 2015-11-09 12:50 +0000
Message-ID <mailman.172.1447073456.16136.python-list@python.org> (permalink)
References <ea008366-9aa2-47e6-b4b7-69e5d447d408@googlegroups.com>

Show all headers | 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