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


Groups > comp.lang.python > #111484

Re: math.frexp

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Random832 <random832@fastmail.com>
Newsgroups comp.lang.python
Subject Re: math.frexp
Date Fri, 15 Jul 2016 14:28:05 -0400
Lines 46
Message-ID <mailman.24.1468607289.2307.python-list@python.org> (permalink)
References <5788cb76$0$1599$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmq=wGopxP_49b6CEjU-N8YbyGeWF85YW+5pe+T_Lsycpg@mail.gmail.com> <mailman.16.1468583297.2307.python-list@python.org> <5789101c$0$1615$c3e8da3$5496439d@news.astraweb.com> <1468607285.2744204.667500217.193B8D54@webmail.messagingengine.com>
Mime-Version 1.0
Content-Type text/plain
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de xtguenPumhru8jKzM2hh2gEX7rZdsudf9oaZOnPKQ+OQ==
Return-Path <random832@fastmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'ma,': 0.09; 'received:internal': 0.09; 'def': 0.13; 'b):': 0.16; 'message- id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'math': 0.20; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'error': 0.27; 'fri,': 0.27; 'correct': 0.28; 'cases.': 0.29; '15,': 0.30; 'relative': 0.30; 'skip:_ 10': 0.32; "d'aprano": 0.33; 'steven': 0.33; 'attempt': 0.35; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'received:66': 0.38; 'mean': 0.38; 'test': 0.39; 'to:addr:python.org': 0.40; 'header:Message-Id:1': 0.61; 'within': 0.64; 'of:': 0.66; 'jul': 0.72; 'answer,': 0.84
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=fPlaFXB/kOiTihvUd7Exry9m0Aw=; b=Ev5t86 rKNL/im5Bhd+NAb1VFnIJiCMaxTV5SnrjEAXGbZN/ABb6zr4VsxXqVRYCWy2GkLj H1q0rIHBn90nr1chXjP1ApW8xbxSH/e4cI6nJJa0uYf2AX3gahmxUn7TviFQLkbg 8Qmc+wUqc8oVKfvyhLqbnIfMG1WsnnzYBXKqk=
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=fPlaFXB/kOiTihv Ud7Exry9m0Aw=; b=RRqGBKxdBFXNS5N0KlOuwSfF9JRCGNDUa4UxapAxAbfaVED QuPDMGAKtk2MGupBkIQDZexUGrxcsnmqI/9qoYBksCTy2ixag8+xF8qn57zcFzYk YcxISKgnZwQREmwmVTnU/18gJ8MY5DUM2FSz7oaKmu3A8cSHGtm2Xierk28E=
X-Sasl-Enc fxZj84g7qp/JoLJCDw2wtS9r+rwM78tbFhujAnDs++n5 1468607285
X-Mailer MessagingEngine.com Webmail Interface - ajax-bf4e2c8f
In-Reply-To <5789101c$0$1615$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <1468607285.2744204.667500217.193B8D54@webmail.messagingengine.com>
X-Mailman-Original-References <5788cb76$0$1599$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmq=wGopxP_49b6CEjU-N8YbyGeWF85YW+5pe+T_Lsycpg@mail.gmail.com> <mailman.16.1468583297.2307.python-list@python.org> <5789101c$0$1615$c3e8da3$5496439d@news.astraweb.com>
Xref csiph.com comp.lang.python:111484

Show key headers only | View raw


On Fri, Jul 15, 2016, at 12:32, Steven D'Aprano wrote:
> I can take the geometric mean of: 
> 
> [2.0**900, 2.0**920, 2.0**960, 2.0**980, 2.0**990]
> 
> and get to within a relative error of 1e-14 of the correct answer,
> 2**950:
> 
> py> geometric_mean([2.0**900, 2.0**920, 2.0**960, 2.0**980, 2.0**990])
> 9.516908214257811e+285
> py> 2.0**950
> 9.516908214257812e+285
>
> Here's one that's exact:
> 
> py> geometric_mean([2e300, 4e300, 8e300, 16e300, 32e300])
> 8e+300

My own attempt gave an exact result for both test cases.

from math import frexp as _frexp, ldexp

def frexp(x):
    if type(x) is tuple:
        m, e1 = x
        m, e2 = _frexp(m)
        return m, e1 + e2
    else:
        return _frexp(x)

def _mul(a, b):
    ma, ea = frexp(a)
    mb, eb = frexp(b)
    return frexp((ma * mb, ea + eb))

def _product(values):
    return reduce(_mul, values)

def geometric_mean(values):
    n = len(values)
    pm, pe = _product(values)
    me, re = divmod(pe, n)
    pm = ldexp(pm, re)
    return ldexp(pm**(1./n), me)

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


Thread

math.frexp Steven D'Aprano <steve@pearwood.info> - 2016-07-15 21:39 +1000
  Re: math.frexp Chris Angelico <rosuav@gmail.com> - 2016-07-15 21:48 +1000
    Re: math.frexp Steven D'Aprano <steve@pearwood.info> - 2016-07-16 02:32 +1000
      Re: math.frexp Chris Angelico <rosuav@gmail.com> - 2016-07-16 02:44 +1000
      Re: math.frexp Random832 <random832@fastmail.com> - 2016-07-15 14:28 -0400
      Re: math.frexp Paul Rubin <no.email@nospam.invalid> - 2016-07-15 13:24 -0700
        Re: math.frexp Steven D'Aprano <steve@pearwood.info> - 2016-07-16 20:21 +1000
          Re: math.frexp Paul Rubin <no.email@nospam.invalid> - 2016-07-16 12:49 -0700
  Re: math.frexp Nobody <nobody@nowhere.invalid> - 2016-07-15 17:39 +0100
  Re: math.frexp Vlastimil Brom <vlastimil.brom@gmail.com> - 2016-07-17 01:18 +0200

csiph-web