Optimizing Memory Allocation in a Simple, but Long Function

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Apr 26 05:54:49 EDT 2016


On 25 April 2016 at 15:35, Derek Klinge <schilke.60 at gmail.com> wrote:
>
> Although I see the value of relative error, I am just as interested in
> absolute error (though admittedly they are directly related values).

I was referring to relative error because the relative error is the
same at each step making the calculation of the global error easier.

> Are there modules or libraries I can/should use to minimize rounding error
> and use very large values of N and get an accurate answer?

from decimal import Decimal, localcontext

def e(prec):
    with localcontext() as ctx:
        ctx.prec = 2*prec + 1
        N = Decimal(10)**(prec)
        eapprox = (1 + 1/N)**N
        ctx.prec = prec
        return +eapprox

print(e(50))

Alternatively you can just use Decimal(1).exp().

> How does the math
> module calculate the vale of e?

https://hg.python.org/cpython/file/tip/Include/pymath.h#l54

--
Oscar



More information about the Python-list mailing list