Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'algorithm': 0.03; 'exception': 0.03; 'expressions': 0.07; '0.1': 0.09; 'sat,': 0.15; '0.3': 0.16; 'comparison.': 0.16; 'digits.': 0.16; 'ever!': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inverse': 0.16; 'measured': 0.16; 'oct': 0.16; 'subject:fails': 0.16; 'wrote:': 0.17; 'examples': 0.18; 'issue.': 0.20; 'received:209.85.214.174': 0.21; 'fine,': 0.22; 'header:In-Reply- To:1': 0.25; 'values': 0.26; 'am,': 0.27; 'rules': 0.27; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; 'actual': 0.28; 'run': 0.28; '(possibly': 0.29; 'arithmetic': 0.29; "d'aprano": 0.29; 'equality': 0.29; "people's": 0.29; 'statements': 0.29; 'steven': 0.29; 'usually': 0.30; 'e.g.': 0.30; 'point': 0.31; "aren't": 0.33; 'problem': 0.33; 'to:addr:python- list': 0.33; 'likely': 0.33; 'received:google.com': 0.34; 'doing': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'useful': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'lost': 0.60; 'real': 0.61; 'life,': 0.62; 'more': 0.63; 'fulfill': 0.65; 'of:': 0.65; 'actually,': 0.84; 'calculations': 0.84; 'find.': 0.84; 'hardly': 0.84; 'warrants': 0.84 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=Q6/c+eIXAANxRh23tTfYvkwiTBcbb7FJCX6UKUXfRYI=; b=MD/fT/VnBvICVsj3F67haddnECZEuRzfHo/lxh+9e4+6UvH62B8Fd2zRP9IgBX5R77 Me5HGrOJBPioz7ohyZBUCc0GHKB9UEzZgMgyawOReDHNGuutxxXvn9KWfh0t3SQyL5EI Ll89x7tFXn1JZDpUPLx9CiMm5MHYVfMf70W6wR6QWxbrAhP2hUVi1U1zy4EN0LF24Yo0 tkak+Itbhe5BIFDVptRgXrWgwRWpvxUSXdooZLISQAFee/2J5kqZAvMIVExx0ZobkRxO S9Vc6hOYpL7/UWEfjIun3+kbO7UrOi/R3N6fwxQknX5T/Iib7hvjjy4PBzaflhnLgT/8 JY7Q== MIME-Version: 1.0 In-Reply-To: <508ab917$0$29967$c3e8da3$5496439d@news.astraweb.com> References: <5089F33A.8010804@mrabarnett.plus.com> <508ab917$0$29967$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 27 Oct 2012 03:45:46 +1100 Subject: Re: a.index(float('nan')) fails 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351269949 news.xs4all.nl 6942 [2001:888:2000:d::a6]:33621 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32227 On Sat, Oct 27, 2012 at 3:23 AM, Steven D'Aprano wrote: > In real life, you are *much* more likely to run into these examples of > "insanity" of floats than to be troubled by NANs: > > - associativity of addition is lost > - distributivity of multiplication is lost > - commutativity of addition is lost > - not all floats have an inverse > > e.g. > > (0.1 + 0.2) + 0.3 != 0.1 + (0.2 + 0.3) > > 1e6*(1.1 + 2.2) != 1e6*1.1 + 1e6*2.2 > > 1e10 + 0.1 + -1e10 != 1e10 + -1e10 + 0.1 > > 1/(1/49.0) != 49.0 > > Such violations of the rules of real arithmetic aren't even hard to find. > They're everywhere. Actually, as I see it, there's only one principle to take note of: the "HMS Pinafore Floating Point Rule"... ** Floating point expressions should never be tested for equality ** ** What, never? ** ** Well, hardly ever! ** The problem isn't with the associativity, it's with the equality comparison. Replace "x == y" with "abs(x-y)