Help with sympy, please

Fredrik Johansson fredrik.johansson at gmail.com
Sun Nov 18 20:33:07 EST 2007


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.

Fredrik



More information about the Python-list mailing list