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-7-bit Path: g2news1.google.com!news1.google.com!postnews.google.com!j33g2000pri.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: How to check a Float for NaN Date: Wed, 30 Apr 2008 14:53:54 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <3132e38d-18bb-4890-9cec-31056ac6e3ba@x19g2000prg.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1209592434 8466 127.0.0.1 (30 Apr 2008 21:53:54 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 30 Apr 2008 21:53:54 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: j33g2000pri.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:21152 Date: 2008-04-30T14:53:54-07:00 List-Id: On Apr 30, 1:36 pm, Jerry wrote: > Check this out: > > function Is_NaN(x : Long_Float) return Boolean is > begin > return x /= x; > end Is_NaN; Maybe that will work, but I wouldn't count on it. First of all, you don't know that the compiler will actually do anything. It may get clever and decide that this is trivially False, and thus generate code that doesn't even look at x. Second, if it generates a "floating- point comparison" instruction, you may not get the behavior you think you're getting. My 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'". Which 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". (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. You're welcome to try it, of course, but don't be surprised or disappointed if it doesn't work. -- Adam