Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.07; 'float': 0.07; 'method.': 0.07; 'builtin': 0.09; 'constructor': 0.09; 'rounds': 0.09; 'subject:module': 0.09; 'whatever.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '12:07,': 0.16; 'accepts': 0.16; 'cc:name:python list': 0.16; 'compute': 0.16; 'correctness': 0.16; 'idea:': 0.16; 'mark,': 0.16; 'merged': 0.16; 'points:': 0.16; 'precision.': 0.16; 'rounding': 0.16; 'unary': 0.16; 'folks': 0.16; 'wrote:': 0.18; 'library': 0.18; 'module': 0.19; 'written': 0.21; '(in': 0.22; 'preferred': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'module,': 0.24; 'passes': 0.24; "shouldn't": 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'url:code': 0.29; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'code': 0.31; 'another.': 0.31; 'decimal': 0.31; 'occurs': 0.31; 'skip:d 20': 0.34; 'created': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'accuracy': 0.36; 'i.e.': 0.36; 'in.': 0.36; 'method': 0.36; 'should': 0.36; 'project': 0.37; 'manager': 0.38; 'e.g.': 0.38; 'fact': 0.38; 'rather': 0.38; 'skip:p 20': 0.39; 'even': 0.60; "you're": 0.61; 'here:': 0.62; "you've": 0.63; 'such': 0.63; 'places': 0.64; 'url:p': 0.64; 'to:addr:gmail.com': 0.65; 'increasing': 0.74; 'restore': 0.78; 'yourself': 0.78; 'oscar': 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:content-type; bh=7tIJSePsqfSrw9rJSEHUPwA9EScaq6BELMkgx4AB2So=; b=Q/NG6GmVb/rUfyQhhv9Ozv5TbCgmqAQyvHGb01LkQN0F+Q3ra8N3Lk9RLkSRKzKzRn F+OySee6sd9IFzQaND0cFHJhinvYazz60Uh9nfG9uvhTn6DR+rXVdpuFqm6GkkgN4RWR 39zHxeympu7qNEdTSZkcwFLsL3yHch6Atj0fWG3a5RwpDMRX6YdbRl/CHMyDWCA3dJwx wPtHNYs2QA4V53aqiEF5pYzG16As9kMVdoAumleHRyth+vETAV61KyordV6lh7sz9v8w NmzCUox/9wk1sxJaiyAM5H7K74iT57cUU1ERKHV7pSa0jOLP2OAKu/eln3H6vLH8r1jp GnTw== X-Received: by 10.52.250.236 with SMTP id zf12mr975828vdc.9.1393512202965; Thu, 27 Feb 2014 06:43:22 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <55525f2c-fd3a-4927-b642-2dbf5eae7e9b@googlegroups.com> References: <02cdd9c7-aef7-4cc7-a813-cd1c9627ceb4@googlegroups.com> <94b1962a-0004-4c5b-b484-972a166b88b5@googlegroups.com> <55525f2c-fd3a-4927-b642-2dbf5eae7e9b@googlegroups.com> From: Oscar Benjamin Date: Thu, 27 Feb 2014 14:42:55 +0000 Subject: Re: extend methods of decimal module To: "Mark H. Harris" Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393512211 news.xs4all.nl 2907 [2001:888:2000:d::a6]:40196 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67143 On 27 February 2014 12:07, Mark H. Harris wrote: > > I have created a project here: > > https://code.google.com/p/pythondecimallibrary/ > > I wrote a dmath.py library module for use with the C accelerated decimal module, that I would like to see merged into the C Python distribution so that folks have it by default... without having to pull it down with GIT, or whatever. Hi Mark, Some points: 1) Why have you committed the code as a .tar.gz file? 2) This function is not such a good idea: def D(numform): return Decimal(str(numform)) The Decimal constructor already accepts strings and many types of numbers. Going via str like this reduces accuracy if e.g. someone passes a float in. 3) In many places you've written code like this: prec=dscale(getcontext().prec +7) sqr = (D(x).sqrt()).__round__(prec) retscale=dscale(prec) The preferred way is: with localcontext() as ctx: ctx.prec += 7 sqr = round(D(x).sqrt(), prec) i.e. use a context manager to restore the context even if an error occurs and use the round builtin rather than the dunder method. 4) You shouldn't be using round/__round__ for precision rounding: it uses decimal places rather than significant figures. If you want to round to context precision just use unary +. i.e.: return +sqr 5) The Decimal.sqrt method already rounds to context precision. There's no need to compute in higher precision and then round it yourself (in fact you're invalidating the correctness of the rounding by double-rounding like this). So really it's just: def sqrt(x): return Decimal(x).sqrt() 6) You should organise it in such a way that you're not progressively increasing the precision each time one function calls another. Oscar