Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.erje.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Gregory Ewing Newsgroups: comp.lang.python Subject: Re: float("nan") in set or as key Date: Sun, 29 May 2011 13:04:32 +1200 Lines: 28 Message-ID: <94dkd3F7k4U1@mid.individual.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net dFb/t5JIiHDofasgDu9JOAobJdG0Mv+ZI2dBEuuLvvTfn4WQN8 Cancel-Lock: sha1:GrQDK5QRsFtNxd7rEF3IVEDWW0o= User-Agent: Mozilla Thunderbird 1.0.5 (Macintosh/20050711) X-Accept-Language: en-us, en In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6506 MRAB wrote: > float("nan") can occur multiple times in a set or as > a key in a dict: > > >>> {float("nan"), float("nan")} > {nan, nan} > > except that sometimes it can't: > > >>> nan = float("nan") > >>> {nan, nan} > {nan} NaNs are weird. They're not equal to themselves: Python 2.7 (r27:82500, Oct 15 2010, 21:14:33) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> nan = float("nan") >>> nan == nan False This confuses the daylights out of Python's dict lookup machinery, which assumes that two references to the same object can't possibly compare unequal, so it doesn't bother calling __eq__ on them. -- Greg