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


Groups > comp.lang.python > #78322

Re: String representations of numbers and approximate equality

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.032
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'string': 0.09; 'cc:addr :python-list': 0.11; 'assume': 0.14; '(eg,': 0.16; 'apologies:': 0.16; 'comparison.': 0.16; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'goal,': 0.16; 'inclined': 0.16; 'padded': 0.16; 'passes,': 0.16; 'periods': 0.16; 'rounding': 0.16; 'subject:String': 0.16; 'suggestion.': 0.16; 'think.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'thu,': 0.19; 'not,': 0.20; 'accepted.': 0.22; 'cc:addr:python.org': 0.22; 'string,': 0.24; 'fine': 0.24; 'looks': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'nearly': 0.26; 'second': 0.26; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'thus': 0.29; 'related': 0.29; 'message- id:@mail.gmail.com': 0.30; 'easier': 0.31; '25,': 0.31; 'decimal': 0.31; 'factor': 0.31; 'int,': 0.31; 'post.': 0.31; 'sep': 0.31; 'subject:numbers': 0.31; 'run': 0.32; 'third': 0.33; 'could': 0.34; 'convert': 0.35; 'definition': 0.35; 'no,': 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'acceptable': 0.36; 'thanks': 0.36; "i'll": 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'handle': 0.38; 'sure': 0.39; 'either': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'catch': 0.60; 'dave': 0.60; 'most': 0.60; 'length': 0.61; 'numbers': 0.61; "you're": 0.61; 'first': 0.61; 'pick': 0.64; 'between': 0.67; 'close': 0.67; 'misses': 0.84; 'notably,': 0.84; 'round.': 0.84; 'angel': 0.91; 'involved.': 0.91; 'round,': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=NgzhOkjSIAkxA+uRJpBcFQDy6c5nhf0l6+CFWxKfgE4=; b=IbKEWC7RBQOgc0v8PZMNZpDl5g1ITFH40gppu1SJP3cVBCldMO/6H7Bu9kDPbSKC7D lwVEFos2Jc27MV3ChZ0KEKK71zB98XphUVIcvojAg8pTHs7kZA3UmkMgwIjajjev7BfW Y2zwBZsqp0Hd2W7cbzvYAc10yQ3icHSOP4pb6cYtTzViHMhDHt5az/pMnMKavlq6N8LG Gh2jPlJ8HvkafxSILMAC5qDAGLlbhyuYaShLFQHbHpkcAp7KxrS9ctke2JCqMxHW5uVq PmEV9q1JlUDO9oh6hJH5JAJ2s2ArwqfVZ8yDWSJjPVbpzmv7V97xbUpcgLuyE65OuwIq WmYg==
MIME-Version 1.0
X-Received by 10.50.117.65 with SMTP id kc1mr18070996igb.34.1411609107193; Wed, 24 Sep 2014 18:38:27 -0700 (PDT)
In-Reply-To <lvv7fq$dem$1@ger.gmane.org>
References <CAPTjJmpOxK5xosvk-=0nBdpsXoEwoSj5qEyY5pf1yFS2PbEoww@mail.gmail.com> <lvv7fq$dem$1@ger.gmane.org>
Date Thu, 25 Sep 2014 11:38:27 +1000
Subject Re: String representations of numbers and approximate equality
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.14339.1411609110.18130.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1411609110 news.xs4all.nl 2879 [2001:888:2000:d::a6]:42299
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78322

Show key headers only | View raw


On Thu, Sep 25, 2014 at 5:56 AM, Dave Angel <davea@davea.name> wrote:
> Your definition is not nearly as concrete as you think. Is the
>  first number considered to be exact, and we'll only check the
>  second? Will the factor always be an int, and thus
>  exact?

Apologies: the definition is concrete, just under-specified in the post.

No, both numbers are inexact; most notably, one third of the factor
might be involved - both of these should be correct:

["1.333", "40"]
["1.33", "40"]

But if it's easier to assume that one or t'other must be exact, that's
fine too - I can just run the check twice, once each way.

> When the desired second number is exactly halfway between two
>  values, is rounding down as acceptable as up?
>   (Eg, exact answer 142.75, acceptable 142.7 and 142.8)

Yes, either would be accepted.

> Once you're sure of the goal, it's a question of whether the
>  decimal package can handle it. I would be inclined to do it in
>  two stages. Drop all the periods and convert each to int (long).
>  Multiply and Add 5**n, where n is related to the length of the
>  target. Convert to string,  padded with lots of zeroes.
>
>
> See if the exact string begins with the target. If not, they're
>  not close enough.
>
> If the first part passes, you just have to check the position of
>  the decimal point.
>
> Or you could check the implementation of round. If you pick the
>  right value for round,  you could do an exact comparison.
>
>
> Alternatively,  if you're just looking to catch typos,  it could
>  be simpler. Or if you put constraints on the numbers
>  involved.

It is just to catch typos. I don't mind if it misses some cases, not a big deal.

Thanks for the suggestion. I'll see how that looks in code.

ChrisA

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


Thread

Re: String representations of numbers and approximate equality Chris Angelico <rosuav@gmail.com> - 2014-09-25 11:38 +1000

csiph-web