Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67143

Re: extend methods of decimal module

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 <oscar.j.benjamin@gmail.com>
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> <mailman.7160.1392836388.18130.python-list@python.org> <94b1962a-0004-4c5b-b484-972a166b88b5@googlegroups.com> <CAKJDb-NaZmLysq+2KWgd_f4UPKCk7jdLZkvcCM6ECL5TSAwCRQ@mail.gmail.com> <mailman.7165.1392847869.18130.python-list@python.org> <55525f2c-fd3a-4927-b642-2dbf5eae7e9b@googlegroups.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Thu, 27 Feb 2014 14:42:55 +0000
Subject Re: extend methods of decimal module
To "Mark H. Harris" <harrismh777@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7422.1393512211.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 27 February 2014 12:07, Mark H. Harris <harrismh777@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-19 07:30 -0800
  Re: extend methods of decimal module Terry Reedy <tjreedy@udel.edu> - 2014-02-19 13:59 -0500
    Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-19 13:30 -0800
      Re: extend methods of decimal module Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-19 15:54 -0600
      Re: extend methods of decimal module Terry Reedy <tjreedy@udel.edu> - 2014-02-19 17:10 -0500
        Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 04:07 -0800
          Re: extend methods of decimal module Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-27 14:42 +0000
            Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 07:42 -0800
              Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 02:57 +1100
              Re: extend methods of decimal module Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-27 16:24 +0000
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 15:00 -0800
                Re: extend methods of decimal module Wolfgang <xpysol@gmail.com> - 2014-02-27 15:43 -0800
                Re: extend methods of decimal module Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-27 23:50 +0000
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 18:15 -0800
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 15:26 +1100
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 21:18 -0800
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 16:26 +1100
                Re: extend methods of decimal module Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-02-28 08:54 +0000
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 10:23 -0800
                Re: extend methods of decimal module Steven D'Aprano <steve@pearwood.info> - 2014-02-28 03:15 +0000
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 20:41 -0800
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 16:00 +1100
                Re: extend methods of decimal module Steven D'Aprano <steve@pearwood.info> - 2014-02-28 07:34 +0000
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 19:52 +1100
                Re: extend methods of decimal module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-28 15:11 +0000
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-03-01 02:36 +1100
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 10:34 -0800
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-03-01 05:37 +1100
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 11:26 -0800
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 11:39 -0800
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 12:17 -0800
          Re: extend methods of decimal module Terry Reedy <tjreedy@udel.edu> - 2014-02-27 12:07 -0500
            Re: extend methods of decimal module Anssi Saari <as@sci.fi> - 2014-03-01 08:55 +0200
              Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-01 16:49 -0800
          Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 04:48 +1100
      Re: extend methods of decimal module Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-19 16:27 -0600
      Re: extend methods of decimal module casevh@gmail.com - 2014-02-19 21:11 -0800
        Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 02:33 -0800
          Re: extend methods of decimal module casevh@gmail.com - 2014-02-28 06:23 -0800
  Re: extend methods of decimal module Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-19 22:29 +0000
    Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 02:37 -0800

csiph-web