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


Groups > comp.lang.python > #74193

Re: NaN comparisons - Call For Anecdotes

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.022
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'example:': 0.03; 'cc:addr :python-list': 0.11; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nan': 0.16; 'other:': 0.16; 'subclass': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'skip:p 40': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'comparing': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; '>>>>': 0.31; 'keyerror:': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'false': 0.36; 'recent': 0.39; 'skip:n 10': 0.64; 'different': 0.65; 'jul': 0.74; 'subject:For': 0.78; 'float,': 0.84; '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=NpC4RrOD+3bA0uKeAJv/Egi1Du0u/I45xdJSb10xBcY=; b=Wu36Xpp1UgDdjKZ224UaVeUAiIoVUo5kdPTEqq/k+XLb+fl1pwHBzs5DB/37qlEoRK xhapDy+To1djilWMyVIwl8vFwVlZ52gfQmFFh3NIpJW2HE72W81DidCMrn5M0tzqx4Fc O16ccSYvACzHralnSKb8b5OBbs+BWEkVAADXU3F9OF2jL8yLbSPwyYIEaaib8msqe525 7lI5/4Wc1Wz7HMV8U9M4AhygasgJgzhesYTMAqBl8ATbpS06UX7HL0eovW4sONVVbhbl WVb4gQ1Fdsq0+Ckn/QCYfpFybEQqJd5GrEoctKRyDYuMZY/H8koTo0J31tOE+9+8SjSn gkYQ==
MIME-Version 1.0
X-Received by 10.58.150.1 with SMTP id ue1mr34500433veb.11.1404842245840; Tue, 08 Jul 2014 10:57:25 -0700 (PDT)
In-Reply-To <CAPTjJmoDZV-60oM6-ehQAMJUZ_gHyGvEKDh-6LBCAqJZLopQTA@mail.gmail.com>
References <53BC05FB.4050707@jmunch.dk> <mailman.11630.1404832805.18130.python-list@python.org> <878uo3akqy.fsf@elektro.pacujo.net> <mailman.11640.1404836832.18130.python-list@python.org> <871ttvahaq.fsf@elektro.pacujo.net> <CAPTjJmoDZV-60oM6-ehQAMJUZ_gHyGvEKDh-6LBCAqJZLopQTA@mail.gmail.com>
Date Wed, 9 Jul 2014 03:57:25 +1000
Subject Re: NaN comparisons - Call For Anecdotes
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.11647.1404842254.18130.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1404842254 news.xs4all.nl 2860 [2001:888:2000:d::a6]:38727
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74193

Show key headers only | View raw


On Wed, Jul 9, 2014 at 3:54 AM, Chris Angelico <rosuav@gmail.com> wrote:
>>>> class X:
>     def __eq__(self, other):
>         if self is other:
>             print("Comparing against self - I am me!")
>             return True
>         print("Comparing against",other,"-",id(other))
>         return False
>     def __hash__(self):
>         return 0
>
>>>> d[X()]
> Comparing against nan - 18777952
> Comparing against nan - 19624864
> Comparing against nan - 18776272
> Traceback (most recent call last):
>   File "<pyshell#20>", line 1, in <module>
>     d[X()]
> KeyError: <__main__.X object at 0x016B40D0>

Better example: Subclass float, then it can actually *be* a nan.

>>> class NoisyFloat(float):
    def __eq__(self, other):
        print("Comparing",id(self),"against",id(other))
        return super().__eq__(other)
    def __hash__(self):
        return super().__hash__()

>>> d[NoisyFloat("nan")]
Comparing 23777152 against 18777952
Comparing 23777152 against 19624864
Comparing 23777152 against 18776272
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    d[NoisyFloat("nan")]
KeyError: nan

That's comparing nan==nan three types with four different nans.

ChrisA

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


Thread

Re: NaN comparisons - Call For Anecdotes Chris Angelico <rosuav@gmail.com> - 2014-07-09 01:19 +1000
  Re: NaN comparisons - Call For Anecdotes Marko Rauhamaa <marko@pacujo.net> - 2014-07-08 19:16 +0300
    Re: NaN comparisons - Call For Anecdotes Chris Angelico <rosuav@gmail.com> - 2014-07-09 02:27 +1000
      Re: NaN comparisons - Call For Anecdotes Marko Rauhamaa <marko@pacujo.net> - 2014-07-08 20:31 +0300
        Re: NaN comparisons - Call For Anecdotes Chris Angelico <rosuav@gmail.com> - 2014-07-09 03:54 +1000
        Re: NaN comparisons - Call For Anecdotes Chris Angelico <rosuav@gmail.com> - 2014-07-09 03:57 +1000
        Re: NaN comparisons - Call For Anecdotes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-08 18:12 +0000
    Re: NaN comparisons - Call For Anecdotes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-08 18:10 +0000

csiph-web