[SciPy-user] Can SciPy compute ln(640320**3 + 744)/163**.5 to 30 places?

Fernando Perez fperez.net at gmail.com
Mon Jan 15 14:03:05 CST 2007


On 1/15/07, Dick Moores <rdm at rcblue.com> wrote:
> Of, course! I forgot about integer division. Thanks, Vincent and Darren.
>
> But I still don't get the precision:
> ==========================
> # clnumTest3-c.py
> from __future__ import division
> import clnum as n
> n.set_default_precision(40)
> print repr(n.exp(n.log(5/23)*2/7))
> =========================
> gets mpf('0.6466073240654112295',17)
>
> How come?

Because you shoulnd't use

from __future__ import division

in this case, since that will turn 5/23 into a plain float, with
64-bit accuracy (well, 53 bits for the mantissa, really).

In [3]: n.set_default_precision(50)

In [4]: n.exp(n.log(n.mpq(5,23)*n.mpq(2,7)))
Out[4]: mpf('0.06211180124223602484472049689440993788819875776397515527949',55)

or alternatively:

In [7]: n.exp(n.log(n.mpq('5/23')*n.mpq('2/7')))
Out[7]: mpf('0.06211180124223602484472049689440993788819875776397515527949',55)


clnum exposes true rationals, so you can use them for a computation
such as this one.

In this regard SAGE has the convenience that it preparses your input
to apply on-the-fly conversions so that all objects are treated either
as rationals or as extended-precision floats.  This has a performance
impact so it's not a good choice for everyday numerics, but it does
save a lot of typing and makes it a bit more convenient for this kind
of thing:

sage: r=RealField(200)
sage: r(5/3)**(r(2/7))
1.1571385359117507748889046340135881102888601841088600244770

Which of the two approaches (full SAGE or clnum/mpfr inside regular
python) suits your needs best is a question only you can answer.

Cheers,

f

ps - due to security problems, the public SAGE notebook is
unfortunately down at the moment.


More information about the SciPy-user mailing list