From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,772ddcb41cd06d5b X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!j33g2000pri.googlegroups.com!not-for-mail From: Jerry Newsgroups: comp.lang.ada Subject: Re: How to check a Float for NaN Date: Wed, 30 Apr 2008 18:05:41 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <3132e38d-18bb-4890-9cec-31056ac6e3ba@x19g2000prg.googlegroups.com> NNTP-Posting-Host: 75.171.61.127 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1209603941 11408 127.0.0.1 (1 May 2008 01:05:41 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 1 May 2008 01:05:41 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: j33g2000pri.googlegroups.com; posting-host=75.171.61.127; posting-account=x5rpZwoAAABMN2XPwcebPWPkebpwQNJG User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:21158 Date: 2008-04-30T18:05:41-07:00 List-Id: On Apr 30, 2:53=A0pm, Adam Beneschan wrote: > On Apr 30, 1:36 pm, Jerry wrote: > > > Check this out: > > > function Is_NaN(x : Long_Float) return Boolean is > > begin > > =A0 =A0 return x /=3D x; > > end Is_NaN; > > Maybe that will work, but I wouldn't count on it. =A0First of all, you > don't know that the compiler will actually do anything. =A0It may get > clever and decide that this is trivially False, and thus generate code > that doesn't even look at x. =A0Second, if it generates a "floating- > point comparison" instruction, you may not get the behavior you think > you're getting. =A0My Pentium manual, for instance, says of the FCOM* > (Compare Real) instructions: "If either operand is a NaN or is in an > undefined format, ... the invalid-operation exception is raised, and > the condition bits are set to 'unordered'". =A0Which means that (on that > processor) the comparison attempt will probably fault, and if it > doesn't because the fault is masked, the condition-code bits will be > set to an "unordered" relation that your compiler may not be > expecting, which means that it may not be translated to "inequality" > the way you think it will. > > > A couple of minutes on Wikipedia saves the day. Fromhttp://en.wikipedia.= org/wiki/NaN#NaN_encodings: > > > "A NaN does not compare equal to any floating-point number or NaN, > > even if the latter has an identical representation. One can therefore > > test whether a variable has a NaN value by comparing it to itself." > > But if you read further, you'll find that signaling NaNs "should raise > an invalid exception". =A0(Why the Pentium FCOM instructions appear to > raise exceptions for quiet NaN's as well as signed NaN's, I don't > know.) > > Anyway, Wikipedia describes the IEC 559/IEEE 754 standard, but as > we've already discussed, (1) Ada doesn't fully support this standard > and (2) NaN's aren't valid values in Ada, so you can't reliably use > the IEC/IEEE standard to predict what will happen if you do this in > Ada. =A0You're welcome to try it, of course, but don't be surprised or > disappointed if it doesn't work. > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-- Adam= Adam, Your points are well made. For "normal" use my little hack should be considered suspect. For my trivial use of getting this pesky binding example code out of my hair, I think it should be OK. Just for completeness, I suppose, I'll add (as I alluded to in my original post), that this: dum1 :=3D 0.0; dum2 :=3D 0.0; Put_Line(Long_Float'image(dum1 / dum2)); outputs this: NaN***************** Certainly not 754 compliance, but perhaps a bit of "awareness." Jerry