Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #111469
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: math.frexp |
| Date | Fri, 15 Jul 2016 21:48:10 +1000 |
| Lines | 39 |
| Message-ID | <mailman.16.1468583297.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> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de uiqXb8L5J7EN2Onlh8JwiQYVbM1BEmsDs8wPgqciZqkw== |
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.012 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'float': 0.05; 'cc:addr :python-list': 0.09; 'def': 0.13; '(also': 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'naive': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'twice.': 0.16; 'values:': 0.16; 'wrote:': 0.16; 'math': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'appears': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'scale': 0.27; 'message-id:@mail.gmail.com': 0.27; 'looks': 0.29; 'end,': 0.29; '15,': 0.30; 'getting': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'changing': 0.34; 'this?': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'expected': 0.35; 'but': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'version': 0.38; 'skip:p 20': 0.38; 'still': 0.40; 'your': 0.60; 'back': 0.62; 'jul': 0.72; 'gain': 0.82; 'chrisa': 0.84; 'to:none': 0.91 |
| 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:cc; bh=CYuD5JaftW6U9ba2FWSuO3OeLrGRzDnO4u1iSKZ80mM=; b=eDGTZSXKaURCznB6swBCGoZqAVDUANn56Mh3KcRRk1IdPgeLiMYscUxzMwc7sjhkr5 8hdtDdPG5rebWNDACsq9NOcTFk9+z8Qq6z75tLQqrZ7W0jsfnj6UNsw+EdipJMW34A2u Oeawin5Ya9G9/bUrYeuUmrzD3OGgYb13MsLdqQv3ZEP08b0pG8BHZsVF9BZelBSq1MOK eEkQoqEDKZOI+qkcrm+CYjMBEwzSNz/EvGg8PUNTjLNOpIa+ieYfiHQmqKSmQckU2rYl xzALC9EXW6ta6ofSdx5vwHrC1MVjnsKYd1PKlIrt0IRxihg0FnQ189pdDUG7GcdbPXS4 eUKA== |
| 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:cc; bh=CYuD5JaftW6U9ba2FWSuO3OeLrGRzDnO4u1iSKZ80mM=; b=KsnxoaaBzjEBnQ/x7b8bRPbchReCtZFhk00FNhMoigL3spOWIyR2DybgsFY5rK049p qIb2qsqY8VyTmWVFYF35nC1pzkUXRMlIJQAmxfx9IZCZVxE/WubvDmcGm/I3K6XJXHV3 yCesRLHZk7/PEb9uC+KDdTV+SLhn8mXCfcjq12IKSr0qmwe97g8+yQLywehFOIUico5C TUNNDWheXgi5qQ440Gwus9ekmGEmJadsECzlfGLTNupc5oSuH/RJRtfRQ1FSOMJ6R9Da xU/zWjjHAAUwj6HSu3VD4G0evvZsNGRgJQ8scjYxRB78oRTw/aEy5/rzS1kKU6FdAO2c vGhA== |
| X-Gm-Message-State | ALyK8tI29/xCzWTx4aDmgyt6Gb/dm3T14BtGR6jW2TLGNygH0Moy7//zhrd3MT6VDFataCb2mOkGiXdjlkQa2w== |
| X-Received | by 10.194.117.72 with SMTP id kc8mr372605wjb.30.1468583290524; Fri, 15 Jul 2016 04:48:10 -0700 (PDT) |
| In-Reply-To | <5788cb76$0$1599$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 | <CAPTjJmq=wGopxP_49b6CEjU-N8YbyGeWF85YW+5pe+T_Lsycpg@mail.gmail.com> |
| X-Mailman-Original-References | <5788cb76$0$1599$c3e8da3$5496439d@news.astraweb.com> |
| Xref | csiph.com comp.lang.python:111469 |
Show key headers only | View raw
On Fri, Jul 15, 2016 at 9:39 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> py> from math import frexp
> py> x = 2.5
> py> y = 3.5
> py> x*y
> 8.75
> py> m1, e1 = math.frexp(x)
> py> m2, e2 = math.frexp(y)
> py> m1*m2 * 2.0**(e1 + e2)
> 8.75
>
>
> Looks good to me. So let's try a less naive version of product():
>
>
> def product_scaled(values):
> scale = 0
> prod = 1.0
> for a in values:
> m1, e1 = math.frexp(a)
> m2, e2 = math.frexp(prod)
> scale += (e1 + e2)
> prod *= (m1*m2)
> return (prod * 2.0**scale)
>
>
> py> product_scaled([2.5, 3.5]) # expected 8.75
> 2.734375
>
You're chaining your product twice. (Also your scale, although that
appears to be correct.) Changing it to "prod = m1 * m2" gives 8.75.
But what do you gain by this? You're still stuffing the result back
into a float at the end, so all you do is change from getting
float("inf") to getting OverflowError. How can you make it not
overflow?
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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