comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion
Date: Fri, 16 Jan 2009 07:11:40 -0800 (PST)
Date: 2009-01-16T07:11:40-08:00	[thread overview]
Message-ID: <a7f81389-7365-4f16-9b7c-7e272eb37826@v5g2000prm.googlegroups.com> (raw)
In-Reply-To: 9069fcf7-4257-4439-ad4a-8d7c8c17f5cf@v5g2000pre.googlegroups.com

On Jan 16, 3:11 pm, ChristopherL <clusard...@aol.com> wrote:
> Thanks Adam, yours is a working solution, see below.
>
> Thanks everyone,
> Chris L.
>
> On Jan 15, 6:27 pm, Adam Beneschan <a...@irvine.com> wrote:
>
>
>
> > I think you'll have to force the compiler to work with 8-bit types
> > instead of 10-bit types.  Since you can't put a 'Size clause on a
> > subtype of Integer, you'll need to declare new types:
>
> >    type Short_Integer1 is range 0 .. (2**8)-1;   --not (2**10)-1
> >    for Short_Integer1'Size use 8;
>
> >    type Short_Integer2 is range -(2**7) .. (2**7)-1;
> >    for Short_Integer2'Size use 8;
>
> > Or, if Short_Integer2 *has* to be a subtype, then declare a new type
> > with a different name, force its size to be 8, and do a type
> > conversion from that new type to Short_Integer2 afterwards.
>
> with Unchecked_Conversion;
>
> with Integer_Text_IO ;
>
> procedure test is
>   type Short_integer1 is range 0.. ((2 ** 8) - 1);
>   for Short_Integer1'Size use 8;
>
>   subtype Short_integer2 is Integer range -(2 ** 7)..((2 ** 7) - 1);
>
>   type Short_Integer3 is range -(2**7) .. (2**7)-1;
>   for Short_Integer3'Size use 8;
>
>   subtype A_Float is Float;
>
>   subtype A_Natural is Natural range 0..((2 ** 8) - 1);
>   -- The next line of code (if used) gives compilation error message:
>   -- A_NATURAL does not denote a first subtype
>   -- for A_Natural'Size use Short_integer1'Size;
>
>   Size_Of_Float : integer := Float'Size;      --32 bits long
>
>   Size_Of_short_integer: integer := Short_integer1'Size;--10 bits
> long
>   Size_Of_short_int: integer := Short_integer2'Size;--10 bits long
>
>   Size_Of_Natural: integer := A_Natural'Size;       --8 bits long
>
>   Arg     : A_Float;
>   Result3 : Short_integer2;
>   Result2 : Short_integer3;
>   Result1 : Short_integer1;
>
>   function Get_Bits2 is new Unchecked_Conversion (Source =>
>     Short_integer1, Target => Short_integer3);
>
> Begin
>
> Arg := 200.0;
>
> Result1 := Short_integer1(Arg);  -- Result1 becomes 200
> Result2 := Get_Bits2 (Result1);
> Result3 := Short_integer2(Result2);
> Integer_Text_IO.Put ( Result3 ) ;
> End test;

OK, you've managed to change the floating-point value 200.0 into an
integer value of -56. The bit representations of these two values are
different. (That's because the explicit type conversion of Arg from
type Float to type Short_integer1 changes the representation to
preserve the value).

I'm curious to understand: why did you do that?

PS. I think you can simplify your code into:

declare
   type Unsigned_8 is mod 2 ** 8;
   for Unsigned_8'Size use 8;
   type Signed_8 is range - 2**7 .. 2**7 - 1;
   for Signed_8'Size use 8;
   function To_Signed_8 is new Ada.Unchecked_Conversion (Unsigned_8,
Signed_8);

   Arg : Float := 200.0;
   Result : Signed_8 := To_Signed_8 (Unsigned_8 (Arg));
begin
   Ada.Text_IO.Put (Signed_8'Image (Result));
end;

--
Ludovic Brenta.



  parent reply	other threads:[~2009-01-16 15:11 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com>
2009-01-15 21:22 ` Restarting Tread: Why isn't this program working with Unchecked_Converstion Jeffrey R. Carter
     [not found]   ` <37daf0e6-39b8-4820-a7fc-b6c5decf1ed8@q19g2000yqi.googlegroups.com>
2009-01-16  0:22     ` Jeffrey R. Carter
2009-01-16  1:37 ` Restarting Tread: Why isn't this program working with anon
     [not found] ` <40239b21-b265-467f-9b27-5890fb2f4c67@w1g2000prm.googlegroups.com>
2009-01-16  2:27   ` Restarting Tread: Why isn't this program working with Unchecked_Converstion Adam Beneschan
     [not found]     ` <9069fcf7-4257-4439-ad4a-8d7c8c17f5cf@v5g2000pre.googlegroups.com>
2009-01-16 15:11       ` Ludovic Brenta [this message]
2009-01-16 16:23         ` Martin
     [not found]         ` <70172b19-360c-4eba-815c-ede747c3bcdf@w39g2000prb.googlegroups.com>
2009-01-16 17:24           ` Ludovic Brenta
2009-01-16 17:26           ` Martin
2009-01-16 17:34           ` Georg Bauhaus
2009-01-16 12:18 ` Stuart
2009-01-16 23:14   ` sjw
2009-01-15 21:24 Martin
     [not found] ` <a8ef6226-db87-4c4c-b38e-9dbc77374f4c@t11g2000yqg.googlegroups.com>
2009-01-16  0:45   ` tmoran
2009-01-16  8:02   ` Martin
replies disabled

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