Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'float': 0.05; 'expressions': 0.07; 'commonly': 0.09; 'input,': 0.09; 'worse': 0.09; 'cc:addr:python-list': 0.10; 'assume': 0.11; 'cases': 0.15; '"should"': 0.16; 'accident.': 0.16; 'arbitrarily': 0.16; 'bounds': 0.16; 'comparison.': 0.16; 'did.': 0.16; 'equality.': 0.16; 'expressions,': 0.16; 'hint:': 0.16; 'occur.': 0.16; 'oct': 0.16; 'propagation': 0.16; 'reasonable.': 0.16; 'received:209.85.216.53': 0.16; 'subject:fails': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'unequal': 0.16; 'unhappy': 0.16; 'wrote:': 0.17; 'comparing': 0.17; 'numerical': 0.17; '(in': 0.18; 'do.': 0.21; 'large,': 0.22; 'occurs': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'absolute': 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'machine': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'replace': 0.27; 'small,': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; "d'aprano": 0.29; 'equality': 0.29; 'large.': 0.29; "people's": 0.29; 'statements': 0.29; 'steven': 0.29; 'use?': 0.29; "i'm": 0.29; 'maybe': 0.29; 'fri,': 0.30; 'relative': 0.30; 'function': 0.30; 'error': 0.30; 'stuff': 0.30; 'problem.': 0.32; 'not.': 0.32; "aren't": 0.33; 'problem': 0.33; 'anyone': 0.33; 'equal': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'said,': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'compare': 0.36; 'should': 0.36; 'too': 0.36; 'does': 0.37; 'two': 0.37; 'skip:3 10': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'some': 0.38; 'things': 0.38; 'sure': 0.38; 'shows': 0.38; 'called': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'real': 0.61; 'evaluate': 0.62; 'life,': 0.62; 'close': 0.63; 'worth': 0.63; 'within': 0.64; '26,': 0.65; 'frequency': 0.65; 'fulfill': 0.65; 'square': 0.75; 'you:': 0.75; 'answer:': 0.84; 'cancellation': 0.84; 'catastrophic': 0.84; 'computation.': 0.84; 'correctly?': 0.84; 'good,': 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:to :cc:content-type; bh=b5JSrYYFmQHEU/YM3/QkZvzGr/TESnE0gF0X7uotHdQ=; b=yVBn8xFMHAfABE7wJEPaUKhvNhA03w2SkMMUGHAwylGcubHUy0vcWpy9nECjHdsy8Q npLbzgzVRGCCVuUlFuOAKHtrjOyEV8tRtIjQKQ9ya+F7cusc4cM6Mho7LhAz0lYkUC2q gqbFC0tTThU2gZWF0Iz0ukQBkjvz8GClYvZ13eY4Q4RWHgnd778WrFnCsGuRkXOaM/qi XiaBK/e92NWeEaRgqEJJ6QdVE3pWO49ooCC7RBj905fqnaTiqhVOBlqViyP7GBD61y0l 0r0Ur+YxP0A7v5ZsuBsLg2ABaN5xTmBx40qOKo7BFJsxoEVr2zXXJv+KBuK6BFVRYhoj FhvQ== MIME-Version: 1.0 In-Reply-To: <508ad937$0$29967$c3e8da3$5496439d@news.astraweb.com> References: <5089F33A.8010804@mrabarnett.plus.com> <508ab917$0$29967$c3e8da3$5496439d@news.astraweb.com> <508ad937$0$29967$c3e8da3$5496439d@news.astraweb.com> From: Devin Jeanpierre Date: Fri, 26 Oct 2012 15:17:37 -0400 Subject: Re: a.index(float('nan')) fails To: "Steven D'Aprano" Content-Type: text/plain; charset=UTF-8 Cc: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351279099 news.xs4all.nl 6987 [2001:888:2000:d::a6]:33992 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32233 On Fri, Oct 26, 2012 at 2:40 PM, Steven D'Aprano wrote: >> The problem isn't with the associativity, it's with the equality >> comparison. Replace "x == y" with "abs(x-y)> and all your statements fulfill people's expectations. > > O RYLY? > > Would you care to tell us which epsilon they should use? I would assume some epsilon that bounds the error of their computation. Which one to use would depend on the error propagation their function incurs. That said, I also disagree with the sentiment "all your statements fulfill people's expectations". Comparing to be within some epsilon of each other may mean that some things that are the result of mathematically unequal expressions, will be called equal because they are very close to each other by accident. Unless perhaps completely tight bounds on error can be achieved? I've never seen anyone do this, but maybe it's reasonable. > Hint: *whatever* epsilon you pick, there will be cases where that is > either stupidly too small, stupidly too large, or one that degenerates to > float equality. And you may not be able to tell if you have one of those > cases or not. > > Here's a concrete example for you: > > What *single* value of epsilon should you pick such that the following > two expressions evaluate correctly? > > sum([1e20, 0.1, -1e20, 0.1]*1000) == 200 > sum([1e20, 99.9, -1e20, 0.1]*1000) != 200 Some computations have unbounded error, such as computations where catastrophic cancellation can occur. That doesn't mean all computations do. For many computations, you can find a single epsilon that will always return True for things that "should" be equal, but aren't -- for example, squaring a number does no worse than tripling the relative error, so if you square a number that was accurate to within machine epsilon, and want to compare it to a constant, you can compare with relative epsilon = 3*machine_epsilon. I'm not sure how commonly this occurs in real life, because I'm not a numerical programmer. All I know is that your example is good, but shows a not-universally-applicable problem. It is, however, still pretty applicable and worth noting, so I'm not unhappy you did. For example, how large can the absolute error of the sin function applied to a float be? Answer: as large as 2, and the relative error can be arbitrarily large. (Reason: error scales with the input, but the frequency of the sin function does not.) (In case you can't tell, I only have studied this stuff as a student. :P) -- Devin