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: g2news2.google.com!postnews.google.com!b9g2000prh.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: How to check a Float for NaN Date: Tue, 6 May 2008 15:18:39 -0700 (PDT) Organization: http://groups.google.com Message-ID: <95c1de75-7aa0-4f00-bbc4-6f503a3c7aef@b9g2000prh.googlegroups.com> References: <3132e38d-18bb-4890-9cec-31056ac6e3ba@x19g2000prg.googlegroups.com> <12227360.svS57WvVVs@linux1.krischik.com> <8ee4e946-786c-4faa-8c95-f9027083eb4b@p25g2000pri.googlegroups.com> <97217ff4-aaf3-41ed-986c-8b6c0954e112@l28g2000prd.googlegroups.com> <4820a758$0$548$58c7af7e@news.kabelfoon.nl> 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 1210112319 4394 127.0.0.1 (6 May 2008 22:18:39 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 6 May 2008 22:18:39 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: b9g2000prh.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: g2news2.google.com comp.lang.ada:5729 Date: 2008-05-06T15:18:39-07:00 List-Id: On May 6, 11:45 am, "Wiljan Derks" wrote: > The following code works fine for us: > > type Ieee_Short_Real is > record > Mantisse_Sign : Integer range 0 .. 1; > Exponent : Integer range 0 .. 2 ** 8 - 1; > Mantisse : Integer range 0 .. 2 ** 23 - 1; > end record; > > for Ieee_Short_Real use > record > Mantisse_Sign at 0 range 31 .. 31; > Exponent at 0 range 23 .. 30; > Mantisse at 0 range 0 .. 22; > end record; > > function Valid_Real (Number : Float) return Boolean is > function To_Ieee_Short_Real is > new Ada.Unchecked_Conversion (Float, Ieee_Short_Real); > begin > return To_Ieee_Short_Real (Number).Exponent /= 255; > end Valid_Real; It might make sense to use modular types instead of signed integer types for Mantisse_Sign, Exponent, and Mantisse (or Mantissa, if you want to use English); then you can use "and", "or", and "not" operations to test bits. Actually, I'd probably define a couple constants here: Exponent_Size : constant := 8; Mantissa_Size : constant := 23; type Exponent_Type is mod 2 ** Exponent_Size; type Mantissa_Type is mod 2 ** Mantissa_Size; type Mantissa_Sign_Type is mod 2; Using constants makes it easy to re-implement this for 64- and 80-bit floats. type Ieee_Short_Real is record Mantissa_Sign : Mantissa_Sign_Type; Exponent : Exponent_Type; Mantissa : Mantissa_Type; end record; (I've omitted the rep clause because I think the one above might work only for big-endian and not little-endian machines or vice versa, and I don't feel like figuring it out because I have a cold and my head hurts enough already.) Then, if X is the result of the Unchecked_Conversion: Number is a valid float if X.Exponent /= Exponent_Type'Last Number is an infinity if X.Exponent = Exponent_Type'Last and X.Mantissa = 0 Number is a qNaN if X.Exponent = Exponent_Type'Last and (X.Mantissa and 2**(Mantissa_Size-1) /= 0) Number is a sNaN if X.Exponent = Exponent_Type'Last and X.Mantissa /= 0 and (X.Mantissa and 2**(Mantissa_Size-1) = 0) On the Pentium, Number is a "real indefinite" if X.Exponent = Exponent_Type'Last and X.Mantissa = 2**(Mantissa_Size-1) Number is a denormal if X.Exponent = 0 and X.Mantissa /= 0 Hope this helps someone (and I hope I didn't screw up and get something wrong), -- Adam