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

2015-11-10 Thread Salvatore DI DIO
Thank you very much Peter
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2015-11-09 Thread Peter Pearson
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(5)
> 2.718281748862504
 lim(9)
> 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
9, 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.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2015-11-09 Thread Salvatore DI DIO
Thank you very much Oscar, I was considering using Mapple :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2015-11-09 Thread Salvatore DI DIO
Thank you very much Oscar,I was considerind using Mapple :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2015-11-09 Thread Salvatore DI DIO
Thank you very much Chris

-- 
https://mail.python.org/mailman/listinfo/python-list


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

2015-11-09 Thread Oscar Benjamin
On 9 November 2015 at 12:21, 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(5)
> 2.718281748862504
 lim(9)
> 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
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2015-11-09 Thread Chris Angelico
On Mon, Nov 9, 2015 at 11:21 PM, 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(5)
> 2.718281748862504
 lim(9)
> 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(9)
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(5)
Decimal('2.718281825740763411884758912')
>>> lim(9)
Decimal('2.71828182694665260445479')
>>> lim(9)
Decimal('2.718281556630875980862943027')

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

>>> decimal.getcontext().prec=200
>>> lim(5)
Decimal('2.7182818257407634118847589119866382434352189174535941028176465917058364010909850212743201574998148959449308935216863472501568773422911827403060031110458945666576132256505421665510943751730347310393611')
>>> lim(9)
Decimal('2.718281826946655322736616533366198705073189604924113513719666470682138645212144697520852594221964992741852547403862053248620085931699038380869918694830598723560639286551799819233225160142059050076')
>>> lim(9)
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
-- 
https://mail.python.org/mailman/listinfo/python-list