Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.080 X-Spam-Evidence: '*H*': 0.84; '*S*': 0.00; 'error:': 0.07; 'suppose': 0.07; 'instance.': 0.09; 'percentage': 0.09; 'raises': 0.09; 'accuracy.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guys,': 0.16; 'wrote:': 0.18; 'normally': 0.19; 'thu,': 0.19; 'error': 0.23; 'questions:': 0.24; 'question': 0.24; 'compare': 0.26; 'first,': 0.26; 'somewhere': 0.26; 'header:In- Reply-To:1': 0.27; 'point': 0.28; 'chris': 0.29; 'absolute': 0.30; 'relative': 0.30; 'message-id:@mail.gmail.com': 0.30; 'usually': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'probably': 0.32; 'received:209.85': 0.35; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'picking': 0.36; 'doing': 0.36; 'should': 0.36; 'wrong': 0.37; 'received:209': 0.37; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'though,': 0.39; 'to:addr:python.org': 0.39; 'easy': 0.60; 'lower': 0.61; 'numbers': 0.61; "you're": 0.61; 'more': 0.64; '30,': 0.65; 'it!': 0.67; 'url:v': 0.71; 'url:youtube': 0.71; 'saw': 0.77; 'url:watch': 0.77; '(ie': 0.84; 'average': 0.93; 'technique': 0.93; '2013': 0.98 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:to :content-type; bh=xMIgt80aWFj20nClYqml6xKoHle+Oe/y826T6QW7KUQ=; b=ZEL3JGI9Bcz9qJTq4hs4mc/ZX6vrykyKJGTo5JytKCtoovzyH3SSiJspLlv6CB5qHI fDv10o2+yDR2pMSUDTYE6b3W4dyEtIJadwCVP75477uH0olqMW6dat2r+SwhcoyZX5hg DqyBowttYaqI3btlxIP99Ezeojtu4dsn4Dazs7udLcJTL2bS26tn7EvDH0uDQ0dXRiNt 5yZjEX3c5U7Zr5AlfumGQoOoc2dYLJwdGWfz3NBEzlfg1tu4wuB4XB60sGphCxJ4x696 i6l1Xoc7IVfH28I/Nr9T9M25VVriyPYsOTk0w2iuNJy8oBUjW/cuVaTHmjocqLjT151l kAmg== MIME-Version: 1.0 X-Received: by 10.66.121.132 with SMTP id lk4mr7544303pab.1.1369902563633; Thu, 30 May 2013 01:29:23 -0700 (PDT) In-Reply-To: <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> <51a6e6b8$0$11118$c3e8da3@news.astraweb.com> Date: Thu, 30 May 2013 18:29:23 +1000 Subject: Re: Short-circuit Logic From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369902572 news.xs4all.nl 15976 [2001:888:2000:d::a6]:36225 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46442 On Thu, May 30, 2013 at 3:42 PM, Steven D'Aprano wrote: > On Thu, 30 May 2013 13:45:13 +1000, Chris Angelico wrote: > >> Let's suppose someone is told to compare floating point numbers by >> seeing if the absolute value of the difference is less than some >> epsilon. > > Which is usually the wrong way to do it! Normally one would prefer > *relative* error, not absolute: > > # absolute error: > abs(a - b) < epsilon > > > # relative error: > abs(a - b)/a < epsilon I was picking an epsilon based on a, though, which comes to pretty much the same thing as the relative error calculation you're using. > But using relative error also raises questions: > > - what if a is negative? > > - why relative to a instead of relative to b? > > - what if a is zero? > > The first, at least, is easy to solve: take the absolute value of a. One technique I saw somewhere is to use the average of a and b. But probably better is to take the lower absolute value (ie the larger epsilon). However, there's still the question of what epsilon should be - what percentage of a or b you take to mean equal - and that one is best answered by looking at the original inputs. Take these guys, for instance. Doing the same thing I was, only with more accuracy. http://www.youtube.com/watch?v=ZNiRzZ66YN0 ChrisA