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!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 08:02:05 -0700 (PDT) Organization: http://groups.google.com Message-ID: <93b0d930-102a-4ac4-8b85-48e87d9d3df1@j33g2000pri.googlegroups.com> References: <3132e38d-18bb-4890-9cec-31056ac6e3ba@x19g2000prg.googlegroups.com> <6f7cd771-16b7-4729-9536-2a7d1c28a9cd@2g2000hsn.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 1209567725 28580 127.0.0.1 (30 Apr 2008 15:02:05 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 30 Apr 2008 15:02:05 +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:21140 Date: 2008-04-30T08:02:05-07:00 List-Id: On Apr 30, 3:50 am, christoph.gr...@eurocopter.com wrote: > On 30 Apr., 12:47, christoph.gr...@eurocopter.com wrote: > > > Try Float'Valid (X), see RM 13.9.2 > > Ahem, I meant X'Valid for X of type Float I'm not sure if this will produce the desired results, since it should return False for +/- infinity also. That may be OK with the original poster, or not. The problem here is that if you're dealing with NaN's on purpose, you're not really dealing with Ada, as far as I can tell, unless your purpose is to test for an uninitialized variable (in which case X'Valid makes sense). Otherwise, though, NaN's and infinities are not possible values of floating-point types, and therefore they can't be returned by operations that return floating-point values, even if Float'Machine_Overflows is False. (There are certain passages in the RM that say a result is implementation-defined if the Machine_Overflows attribute is False; however, I do not think assigning a variable to an invalid value, as a NaN would be, is an allowed "implementation-defined" result. 13.9.2(4-11) lists the ways that invalid data could be created, and the "result of a floating- point operation that cannot return a valid result" is not one of those listed. Yes, I know that this is a NOTE and is not normative.) Of course, if you compile with checking turned off or your code does something to turn off the floating-point overflow-checking bit in the processor (if there is one), you're outside the bounds of Ada, so you can't really come up with an Ada solution to the question, and whatever you do won't be portable. In that case, "whatever works" is fine. If I had to do this, I'd just do an Unchecked_Conversion to some data structure with a rep clause that represents an IEEE-754 float, or to a modular integer type or array of modular integer types, and then just test the bits myself. Testing the 'Image for "NaN" will probably work, but if efficiency is a concern I wouldn't do this because if the float is a valid one, it's a *lot* of work to convert the float to a decimal representation, and that's especially painful here because you're just going to throw all that work out. P.S. Has the ARG or anyone discussed the possibility of building support for infinities and/or NaN's into the language---i.e. defining a floating-point type that includes those "values"? G.1.1(56) makes me think that maybe there were some thoughts that this might be done sometime in the future, if there's a demand for it, but I could be reading something into this paragraph that isn't there. -- Adam