Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!news.le-studio75.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'instance,': 0.05; 'behavior,': 0.07; 'raised': 0.07; 'python': 0.08; 'all?': 0.09; 'commonly': 0.09; 'comparisons': 0.09; 'exception.': 0.09; 'exceptions': 0.09; 'exceptions,': 0.09; 'storing': 0.09; 'exception': 0.12; 'am,': 0.14; 'wrote:': 0.14; '+1.': 0.16; 'additions': 0.16; 'angelico': 0.16; 'behaviour,': 0.16; 'bootstrap': 0.16; 'exponent': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inverse': 0.16; 'methods,': 0.16; 'nan': 0.16; 'signaled': 0.16; 'somehow,': 0.16; 'subject:key': 0.16; 'subject:set': 0.16; 'argument': 0.16; "wouldn't": 0.17; 'have,': 0.17; 'mon,': 0.17; 'guess': 0.19; 'header:In-Reply-To:1': 0.21; 'variable': 0.21; 'ignore': 0.21; 'integer': 0.23; 'received:209.85.210.174': 0.23; 'received:mail- iy0-f174.google.com': 0.23; 'url:wiki': 0.23; '(or': 0.24; 'code': 0.24; 'invalid': 0.25; 'function': 0.25; 'string': 0.26; "i'm": 0.27; 'correct': 0.28; 'message-id:@mail.gmail.com': 0.28; 'language.': 0.28; '(you': 0.29; 'instead': 0.29; 'least': 0.30; 'unable': 0.30; 'do.': 0.30; 'carl': 0.30; "one's": 0.30; 'environment': 0.30; 'define': 0.31; 'equivalent': 0.31; 'done': 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'operations': 0.33; 'list': 0.33; 'actually': 0.33; 'things': 0.33; 'rather': 0.34; 'chris': 0.34; 'characters': 0.34; 'there': 0.35; 'conditions.': 0.35; 'trigger': 0.35; 'store': 0.35; 'probably': 0.36; 'none': 0.37; 'too.': 0.37; 'received:google.com': 0.37; 'change': 0.37; 'received:209.85': 0.37; 'floating': 0.37; 'involving': 0.37; 'mathematical': 0.37; 'url:en': 0.37; 'url:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'called': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'current': 0.40; 'meaning': 0.40; 'square': 0.67; 'production': 0.68; 'apart': 0.73; 'unnecessary': 0.73; '30,': 0.84; "(i've": 0.84; '10:55': 0.84; 'adhere': 0.84; 'existed': 0.91; 'lifting': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=KGbciyint7j+qz8143L9EVaFR153NTuqs3MP+jCvAbk=; b=D4f2mHImnY2a44tJ3plbbwMvim4GdJvk0eqNeUNnQDY1ywWNq6I3lET4EffsUtAVs0 2XbB/fI/jTU1LU1Z/mPqWuZEXOkL3JVM8PLq77ZNJvXQMd5B0iWtT46+kJIiMQDH8och 6tpY31tmZCpFuVFhtNBpAprVZr1AwaGs5FQUY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=cv6f1dTHtPAnUnPN5Eij0xfYCXnRa9Pf4YM96Wjuv0CFYEGG9qI47PVMivaA1xZPXc xhZfsVXNNkNpVW1Jl9+32x0IzBWMAtma3BqWTNCU7s4hR5nXeDDpXpbbe+otR6E1i62B xZZRQH1NhVLSssbl5eAYQgl8loewvD6eQSzmk= MIME-Version: 1.0 In-Reply-To: <271acea3-c38f-4123-9038-e348fb841971@glegroupsg2000goo.googlegroups.com> References: <4de2d746$0$29996$c3e8da3$5496439d@news.astraweb.com> <271acea3-c38f-4123-9038-e348fb841971@glegroupsg2000goo.googlegroups.com> Date: Mon, 30 May 2011 11:14:58 +1000 Subject: Re: float("nan") in set or as key From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 53 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306718101 news.xs4all.nl 49176 [::ffff:82.94.164.166]:48015 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6604 On Mon, May 30, 2011 at 10:55 AM, Carl Banks wro= te: > If exceptions had commonly existed in that environment there's no chance = they would have chosen that behavior; comparison against NaN (or any operat= ion with NaN) would have signaled a floating point exception. =A0That is th= e correct way to handle exceptional conditions. > > The only reason to keep NaN's current behavior is to adhere to IEEE, but = given that Python has trailblazed a path of correcting arcane mathematical = behavior, I definitely see an argument that Python should do the same for N= aN, and if it were done Python would be a better language. If you're going to change behaviour, why have a floating point value called "nan" at all? Other than being a title for one's grandmother, what meaning does that string have, and why should it be able to be cast as floating point? Lifting from http://en.wikipedia.org/wiki/NaN a list of things that can return a NaN (I've removed non-ASCII characters from this snippet): * Operations with a NaN as at least one operand. (you need to bootstrap that somehow, so we can ignore this - it just means that nan+1 =3D nan) * The divisions 0/0 and infinity/infinity * The multiplications 0*infinity and infinity*0 * The additions +inf + (-inf), (-inf) + +inf and equivalent subtractions * The standard pow function and the integer exponent pown function define 0**0, 1**inf, and inf**0 as 1. * The powr function define all three indeterminate forms as invalid operations and so returns NaN. * The square root of a negative number. * The logarithm of a negative number * The inverse sine or cosine of a number that is less than -1 or greater than +1. Rather than having comparisons with NaN trigger exceptions, wouldn't it be much cleaner to have all these operations trigger exceptions? And, I would guess that they probably already do. NaN has an additional use in that it can be used like a "null pointer"; a floating-point variable can store 1.0, or 0.000000000005, or "no there's no value that I'm storing in this variable". Since a Python variable can contain None instead of a float, this use is unnecessary too. So, apart from float("nan"), are there actually any places where real production code has to handle NaN? I was unable to get a nan by any of the above methods, except for operations involving inf; for instance, float("inf")-float("inf") =3D=3D nan. All the others raised an exception rather than return nan. Chris Angelico