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


Groups > comp.lang.python > #67226

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!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.047
X-Spam-Evidence '*H*': 0.91; '*S*': 0.00; 'class,': 0.07; 'element': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'element,': 0.16; 'mark,': 0.16; 'message- id:@post.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'storing': 0.16; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'fraction': 0.24; 'cheers,': 0.24; 'certain': 0.27; 'skip:" 20': 0.27; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'url:code': 0.29; 'code': 0.31; '"",': 0.31; 'decimal': 0.31; 'received:132': 0.31; 'writes:': 0.31; 'file': 0.32; 'handled': 0.32; '(most': 0.33; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'charset:us-ascii': 0.36; 'possible': 0.36; 'performance': 0.37; 'to:addr:python-list': 0.38; 'previous': 0.38; 'recent': 0.39; 'extremely': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'new': 0.61; 'numbers': 0.61; 'range': 0.61; 'simple': 0.61; 'url:p': 0.64; 'become': 0.64; 'series': 0.66; 'here': 0.66; 'burden': 0.68; 'boost': 0.70; 'potentially': 0.81; 'calculations': 0.84; 'hands,': 0.84; 'increases': 0.91; 'enhancement': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: extend methods of decimal module
Date Fri, 28 Feb 2014 14:41:49 +0000 (UTC)
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host sea.gmane.org
User-Agent Loom/3.14 (http://gmane.org/)
X-Loom-IP 132.230.1.31 (Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0)
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.7472.1393598536.18130.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1393598536 news.xs4all.nl 2897 [2001:888:2000:d::a6]:58279
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:67226

Show key headers only | View raw


Mark H. Harris <harrismh777 <at> gmail.com> writes:
> 
> If you get a chance, take a look at the  dmath.py  code on:
> 
>    https://code.google.com/p/pythondecimallibrary/ 
> 

Hi Mark,
here is an enhancement for your epx function.
Your current version comes with the disadvantage of potentially storing
extremely large values in n and d because of the multiplications in the
while loop:

    q = D(x)
    n = q
    c = D(1)
    d = D(1)
    ex = 1 + q
    prev_ex = D(0)
    while (ex != prev_ex):
        prev_ex = ex
        c += 1
        d *= c    # can become a huge number
        n *= q    # this as well
        ex += n/d

in general, large numbers are handled well by the Decimal class, but there
is a certain burden on these calculations and with VERY large numbers you
can also get a decimal.Overflow:

>>> exp(200000)
Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    epx(190000)
  File "C:\Python34\dmath_rev.py", line 27, in epx
    n *= q
decimal.Overflow: [<class 'decimal.Overflow'>]

My re-write of the part above is:

    q = D(x)
    c = 1
    new_element = q
    ex = 1 + new_element
    prev_ex = D(0)
    while (ex != prev_ex):
        prev_ex = ex
        c += 1
        # every new element in the series is a known fraction of the
        # previous element,
        # so there is no need to store large numbers
        new_element *= q/c
        ex += new_element

in my hands, this simple change increases performance (exact timing left to
you) and boost the range of possible calculations:

>>> epx2(1000000)
Decimal('3.033215396802087545086402141E+434294')

Cheers,
Wolfgang

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


Thread

Re: extend methods of decimal module Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-02-28 14:41 +0000
  Re: extend methods of decimal module Wolfgang <xpysol@gmail.com> - 2014-02-28 06:52 -0800
  Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 10:17 -0800

csiph-web