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


Groups > comp.lang.python > #32227

Re: a.index(float('nan')) fails

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 <rosuav@gmail.com>
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 <bd80bfd0-b423-418f-a338-fea626d50093@googlegroups.com> <k6cr4c$4md$1@ger.gmane.org> <5089F33A.8010804@mrabarnett.plus.com> <mailman.2886.1351238424.27098.python-list@python.org> <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 <rosuav@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2898.1351269949.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, Oct 27, 2012 at 3:23 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> 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)<epsilon" for some epsilon
and all your statements fulfill people's expectations. (Possibly with
the exception of "1e10 + 0.1 + -1e10" as it's going to be hard for an
automated algorithm to pick a useful epsilon. But it still works.)
Ultimately, it's the old problem of significant digits. Usually it
only comes up with measured quantities, but this is ultimately the
same issue. Doing calculations to greater precision than the answer
warrants is fine, but when you come to compare, you effectively need
to round both values off to their actual precisions.

ChrisA

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


Thread

a.index(float('nan')) fails mamboknave@gmail.com - 2012-10-25 18:46 -0700
  Re: a.index(float('nan')) fails Terry Reedy <tjreedy@udel.edu> - 2012-10-25 22:04 -0400
    Re: a.index(float('nan')) fails Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 02:44 +0000
      Re: a.index(float('nan')) fails Terry Reedy <tjreedy@udel.edu> - 2012-10-26 03:54 -0400
        Re: a.index(float('nan')) fails Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 15:26 +0000
          Re: a.index(float('nan')) fails Terry Reedy <tjreedy@udel.edu> - 2012-10-26 14:49 -0400
    Re: a.index(float('nan')) fails Nobody <nobody@nowhere.com> - 2012-10-27 17:40 +0100
  Re: a.index(float('nan')) fails Cameron Simpson <cs@zip.com.au> - 2012-10-26 13:15 +1100
    Re: a.index(float('nan')) fails mamboknave@gmail.com - 2012-10-25 19:22 -0700
    Re: a.index(float('nan')) fails mamboknave@gmail.com - 2012-10-25 19:22 -0700
  Re: a.index(float('nan')) fails MRAB <python@mrabarnett.plus.com> - 2012-10-26 03:19 +0100
  Re: a.index(float('nan')) fails Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 02:33 +0000
    Re: a.index(float('nan')) fails Ethan Furman <ethan@stoneleaf.us> - 2012-10-28 06:07 -0700
  Re: a.index(float('nan')) fails Terry Reedy <tjreedy@udel.edu> - 2012-10-26 04:00 -0400
    Re: a.index(float('nan')) fails Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 16:23 +0000
      Re: a.index(float('nan')) fails MRAB <python@mrabarnett.plus.com> - 2012-10-26 17:43 +0100
      Re: a.index(float('nan')) fails Chris Angelico <rosuav@gmail.com> - 2012-10-27 03:45 +1100
        Re: a.index(float('nan')) fails Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 18:40 +0000
          Re: a.index(float('nan')) fails Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-26 15:17 -0400
          Re: a.index(float('nan')) fails Chris Angelico <rosuav@gmail.com> - 2012-10-27 12:29 +1100
      Re: a.index(float('nan')) fails Terry Reedy <tjreedy@udel.edu> - 2012-10-26 14:58 -0400
      Re: a.index(float('nan')) fails Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-27 00:48 -0400
        Re: a.index(float('nan')) fails Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-10-27 08:56 +0200
          Re: a.index(float('nan')) fails Nobody <nobody@nowhere.com> - 2012-10-27 17:45 +0100
          Re: a.index(float('nan')) fails Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-27 13:07 -0400
  Re: a.index(float('nan')) fails Mark Adam <dreamingforward@gmail.com> - 2012-10-27 13:33 -0500

csiph-web