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


Groups > comp.lang.python > #74193

Re: NaN comparisons - Call For Anecdotes

References (1 earlier) <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 2014-07-09 03:57 +1000
Subject Re: NaN comparisons - Call For Anecdotes
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.11647.1404842254.18130.python-list@python.org> (permalink)

Show all headers | 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