Help with sympy, please

Dick Moores rdm at rcblue.com
Sun Nov 18 20:45:25 EST 2007


At 05:33 PM 11/18/2007, Fredrik Johansson wrote:
>On Nov 19, 2007 2:23 AM, Dick Moores <rdm at rcblue.com> wrote:
> > OK, I tried mpmath again, and to my surprise, it went well!
> >
> > ===================================
> > #!/usr/bin/env python
> > #coding=utf-8
> > from mpmath import *
> > mpf.dps = 50
> > n = 1
> > k = 0
> > prod = mpf(1)
> > while k < 100000:
> >      k += 1
> >      term = exp(1.0/n)/exp(1.0/(n+1))
> >      prod *= term
> >      n += 2
> > print prod, term
> > ======================================
> > Output:
> > 1.9999950000187499635016028080844735182389158683797
> > 1.0000000000250001250004074790133889386806610626172
>
>You're getting slightly wrong results, though, because 1.0/n and
>1.0/(n+1) just performs regular float division with ~16-digit
>precision when n is a Python int. You should convert either 1.0 or n
>to an mpf before dividing.

Ah, yes. That was my original mistake with using mpmath. How's this?:

=============================
#!/usr/bin/env python
#coding=utf-8
import psyco
psyco.full()
from mpmath import *
mpf.dps = 50
n = mpf(1)
k = 0
prod = mpf(1)
while k < 100000:
     k += 1
     term = exp(1.0/n)/exp(1.0/(n+1))
     prod *= term
     n += 2
print prod, term
===============================
Output:
1.9999950000187499635415917971337956346129920295869 
1.0000000000250001250009375062500416669401059407666

Dick





More information about the Python-list mailing list