Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98513 > unrolled thread
| Started by | Salvatore DI DIO <artyprog@gmail.com> |
|---|---|
| First post | 2015-11-09 04:21 -0800 |
| Last post | 2015-11-10 00:26 -0800 |
| Articles | 8 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Salvatore DI DIO <artyprog@gmail.com> |
|---|---|
| Date | 2015-11-09 04:21 -0800 |
| Subject | Calulation in lim (1 + 1 /n) ^n when n -> infinite |
| Message-ID | <ea008366-9aa2-47e6-b4b7-69e5d447d408@googlegroups.com> |
Hello,
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 ?
Regards
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-11-09 23:45 +1100 |
| Subject | Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite |
| Message-ID | <mailman.171.1447073105.16136.python-list@python.org> |
| In reply to | #98513 |
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
[toc] | [prev] | [next] | [standalone]
| From | Salvatore DI DIO <artyprog@gmail.com> |
|---|---|
| Date | 2015-11-09 07:13 -0800 |
| Subject | Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite |
| Message-ID | <0679c4f1-0e38-4b46-9224-59c95ff6bb44@googlegroups.com> |
| In reply to | #98517 |
Thank you very much Chris
[toc] | [prev] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2015-11-09 12:50 +0000 |
| Subject | Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite |
| Message-ID | <mailman.172.1447073456.16136.python-list@python.org> |
| In reply to | #98513 |
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
[toc] | [prev] | [next] | [standalone]
| From | Salvatore DI DIO <artyprog@gmail.com> |
|---|---|
| Date | 2015-11-09 07:13 -0800 |
| Subject | Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite |
| Message-ID | <2e5327a5-2aa4-4dde-ad37-1d1e97cd9944@googlegroups.com> |
| In reply to | #98518 |
Thank you very much Oscar,I was considerind using Mapple :-)
[toc] | [prev] | [next] | [standalone]
| From | Salvatore DI DIO <artyprog@gmail.com> |
|---|---|
| Date | 2015-11-09 07:15 -0800 |
| Subject | Re: Calulation in lim (1 + 1 /n) ^n when n -> infinite |
| Message-ID | <616e255d-80cf-436a-859d-6f400f61183b@googlegroups.com> |
| In reply to | #98518 |
Thank you very much Oscar, I was considering using Mapple :-)
[toc] | [prev] | [next] | [standalone]
| From | Peter Pearson <pkpearson@nowhere.invalid> |
|---|---|
| Date | 2015-11-09 17:51 +0000 |
| Message-ID | <dac4p6Fq56qU1@mid.individual.net> |
| In reply to | #98513 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Salvatore DI DIO <artyprog@gmail.com> |
|---|---|
| Date | 2015-11-10 00:26 -0800 |
| Message-ID | <11f5750f-9ffb-464c-b2d2-71baa7e26ba3@googlegroups.com> |
| In reply to | #98546 |
Thank you very much Peter
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web