From: Adam Beneschan <adam@irvine.com>
Subject: Re: How to check a Float for NaN
Date: Tue, 6 May 2008 15:18:39 -0700 (PDT)
Date: 2008-05-06T15:18:39-07:00 [thread overview]
Message-ID: <95c1de75-7aa0-4f00-bbc4-6f503a3c7aef@b9g2000prh.googlegroups.com> (raw)
In-Reply-To: 4820a758$0$548$58c7af7e@news.kabelfoon.nl
On May 6, 11:45 am, "Wiljan Derks" <Wiljan.De...@gmail.com> 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
next prev parent reply other threads:[~2008-05-06 22:18 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-04-30 10:27 How to check a Float for NaN Jerry
2008-04-30 10:47 ` christoph.grein
2008-04-30 10:50 ` christoph.grein
2008-04-30 15:02 ` Adam Beneschan
2008-04-30 20:33 ` Jerry
2008-04-30 23:23 ` Adam Beneschan
2008-05-01 1:00 ` Adam Beneschan
2008-05-01 19:52 ` Keith Thompson
2008-05-01 23:57 ` Jerry
2008-04-30 23:29 ` Randy Brukardt
2008-05-01 8:04 ` Stuart
2008-05-01 14:38 ` Adam Beneschan
2008-05-01 17:14 ` Stuart
2008-05-01 19:22 ` Randy Brukardt
2008-05-02 0:04 ` Jerry
2008-04-30 20:36 ` Jerry
2008-04-30 21:53 ` Adam Beneschan
2008-05-01 1:05 ` Jerry
2014-05-22 7:27 ` jan.de.kruyf
2014-05-22 8:09 ` Dmitry A. Kazakov
2014-05-22 9:24 ` Simon Wright
2014-05-22 9:48 ` Dmitry A. Kazakov
2014-05-22 15:28 ` Adam Beneschan
2014-05-22 16:31 ` Dmitry A. Kazakov
2014-05-22 23:33 ` Adam Beneschan
2014-05-23 7:38 ` Dmitry A. Kazakov
2014-05-23 21:39 ` Randy Brukardt
2014-05-27 8:35 ` Dmitry A. Kazakov
2014-05-27 12:35 ` Maurizio Tomasi
2014-05-27 15:53 ` Adam Beneschan
2014-05-27 22:35 ` Randy Brukardt
2014-05-27 22:59 ` Jeffrey Carter
2014-05-28 7:32 ` Dmitry A. Kazakov
2014-05-28 8:40 ` Maurizio Tomasi
2008-05-05 18:23 ` Martin Krischik
2008-05-05 20:49 ` Adam Beneschan
2008-05-06 18:09 ` Jerry
2008-05-06 18:45 ` Wiljan Derks
2008-05-06 22:18 ` Adam Beneschan [this message]
2008-05-07 22:56 ` Randy Brukardt
2008-05-07 23:20 ` Adam Beneschan
2008-05-09 7:24 ` Stephen Leake
2008-05-07 22:56 ` Randy Brukardt
2008-05-10 17:00 ` anon
2008-05-11 22:00 ` Keith Thompson
2008-05-12 2:01 ` anon
2008-05-09 19:49 ` anon
2008-05-10 2:36 ` Jerry
2008-05-10 3:53 ` anon
2008-05-10 6:24 ` christoph.grein
2008-05-10 8:05 ` Georg Bauhaus
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox