comp.lang.ada
 help / color / mirror / Atom feed
From: "Eric G. Miller" <egm2@Jay-Pee-eSs.net>
Subject: Re: Gnat/Windows float point Q
Date: Wed, 09 Apr 2003 06:47:11 GMT
Date: 2003-04-09T06:47:11+00:00	[thread overview]
Message-ID: <pan.2003.04.09.06.46.54.233699@Jay-Pee-eSs.net> (raw)
In-Reply-To: 1MGka.57623$ug3.113346@rwcrnsc51.ops.asp.att.net

In <1MGka.57623$ug3.113346@rwcrnsc51.ops.asp.att.net>, tmora wrote:

> What does it mean when float'image(x) = " 47.720649 0.E+05" ?
>                                                    ???
> 
> Is x a NAN or something?  It causes a Constraint_Error in
> the "return x" statement in:
> 
>   subtype units is float range 0.0 .. 1.0;
>   function clip(x: float) return units is
>   begin
>     if x < 0.0 then return 0.0;
>     elsif x > 1.0 then return 1.0;
>     else return x;
>     end if;
>   end clip;

Check X'Valid if the data comes from some route like Unchecked_Conversion.
The following demonstrates the conversion error on some systems..

with Ada.Text_Io;
use  Ada.Text_Io;
with Ada.Unchecked_Conversion;

procedure Unitarians
is

   subtype Units is Float range 0.0 .. 1.0;

   function Clip(X: Float) return Units is
   begin
      if X'Valid = False then
         raise Constraint_Error;
      end if;
      if X < 0.0 then
         return 0.0;
      elsif X > 1.0 then return 1.0;
      else return X;
      end if;
   end Clip;

   type Byte4 is mod 2**32;

   function To_Float is new
     Ada.Unchecked_Conversion (Byte4, Float);

   U : Units;
   F : Float;
   UI : Byte4 := 16#FFFF_FFFF#;
begin
   Put_Line ("UI = " & Byte4'Image (UI));
   F := To_Float (UI);
   Put_Line ("F is valid? --> " & Boolean'Image(F'Valid));
   U := Clip (F);
   Put_Line ("U = " & Units'Image(U));

end Unitarians;

-- 
echo ">gra.fcw@2ztr< eryyvZ .T pveR" | rot13 | reverse




  reply	other threads:[~2003-04-09  6:47 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-08 21:00 Gnat/Windows float point Q tmoran
2003-04-09  6:47 ` Eric G. Miller [this message]
2003-04-09  8:30   ` tmoran
2003-04-09 21:11     ` Randy Brukardt
2003-04-10  6:23       ` tmoran
2003-04-10 18:39         ` Randy Brukardt
2003-04-10 21:58           ` tmoran
2003-04-11  0:52             ` David C. Hoos, Sr.
2003-04-11  1:37               ` tmoran
2003-04-11  4:24                 ` Eric G. Miller
2003-04-11 19:11         ` Simon Wright
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox