Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: Optimizing Memory Allocation in a Simple, but Long Function Date: Tue, 26 Apr 2016 10:54:49 +0100 Lines: 32 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 0cJ9AmnVk+bC31qkBeygbgXYWSzJ12K0U7tzYaixeCIw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Function': 0.07; 'cc:addr:python-list': 0.09; 'answer?': 0.09; 'rounding': 0.09; 'subject:Long': 0.09; 'def': 0.13; '2016': 0.16; 'cc:name:python list': 0.16; 'prec': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'vale': 0.16; 'wrote:': 0.16; 'math': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'libraries': 0.22; 'referring': 0.22; 'absolute': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'error': 0.27; 'message-id:@mail.gmail.com': 0.27; 'values': 0.28; 'decimal': 0.29; 'relative': 0.30; 'related': 0.32; 'url:python': 0.33; 'subject:Simple': 0.33; 'received:google.com': 0.35; 'step': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'does': 0.39; 'making': 0.62; 'oscar': 0.84; 'url:cpython': 0.84; 'url:include': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=UUSsBEoATUmX/ATg8kpy2sQvvRGRegBHSD0EGgnsR/E=; b=WfQeqJ7yfd8MPcLxSOOQ8rrdvkCq5zSlTkQXY4GTrr4tRptZShcjm0gEG64lNPLjdv onSIUBgt+BYOW7WR1GjVCoqyukoO09s5tRQLAWu5mrbks0BEjDzdJL2roYHumum7XL3s +TLGPAsTe+D2M6DdlbHnqVu2SelsTfbNd54/lgExKLR9bN9sGVLwzmNRnBgKGT7JhbvU kLE8RWY5IL3OJGIQLk++9EV+1ab0bMX1Z1KUEQUmb1l8ru/uUsWfBzGqTqD9nmksF09k 4G8Er6/QMMRlDBLeJki6NpI26DNK/KP2lrAJt9k6ZonlA3hsNtrTPZhBxV4u1t5qzRs3 oFIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=UUSsBEoATUmX/ATg8kpy2sQvvRGRegBHSD0EGgnsR/E=; b=IRnKs7TEgvq4ugqS/4LjH2RciJXDGA6l8As2xmc5O1MuV51OBjsCz1fTsCTc1SAkXd pbKAgXkEo14Mgbs9RVxC0xnQvA2VWyBKKqm50KPx+uJdlGZAMt24YChHk8DxvLRmSCeV VuIsp39Dl3cG9NIg7r1GpPG1IjF5V+hnntV897gbMLciRngk/hJ1QtMOy1vXGXHqapzm ziOopIeta8g+P/wqLTFu6h3f+thb++ZWWeNC24AnzeF6wcpuslBzd+nNb/rt2vTul4JB LV4TV0gi2Y6nfAxENK0S3ZnSpGLllRZmv2BgEWM55eXyG18WpsqffzYPM+ij0CKz1kVT xdVA== X-Gm-Message-State: AOPr4FUWgJAIGYJEFb3RWbTb0sTXiw76KcM2nbK+INVow2aUHIlW+ZCWtZfPvpQZltM/KfKVZ/2tWkp8v7dJPw== X-Received: by 10.112.13.193 with SMTP id j1mr882701lbc.39.1461664509722; Tue, 26 Apr 2016 02:55:09 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:107646 On 25 April 2016 at 15:35, Derek Klinge 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