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,8af7fe40dc0a55ae X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!v5g2000prm.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion Date: Fri, 16 Jan 2009 07:11:40 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com> <40239b21-b265-467f-9b27-5890fb2f4c67@w1g2000prm.googlegroups.com> <5df15a77-b654-41da-bea0-a799f4cb6230@v13g2000yqm.googlegroups.com> <9069fcf7-4257-4439-ad4a-8d7c8c17f5cf@v5g2000pre.googlegroups.com> NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1232118701 20967 127.0.0.1 (16 Jan 2009 15:11:41 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 16 Jan 2009 15:11:41 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: v5g2000prm.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:4356 Date: 2009-01-16T07:11:40-08:00 List-Id: On Jan 16, 3:11=A0pm, ChristopherL wrote: > Thanks Adam, yours is a working solution, see below. > > Thanks everyone, > Chris L. > > On Jan 15, 6:27=A0pm, Adam Beneschan wrote: > > > > > I think you'll have to force the compiler to work with 8-bit types > > instead of 10-bit types. =A0Since you can't put a 'Size clause on a > > subtype of Integer, you'll need to declare new types: > > > =A0 =A0type Short_Integer1 is range 0 .. (2**8)-1; =A0 --not (2**10)-1 > > =A0 =A0for Short_Integer1'Size use 8; > > > =A0 =A0type Short_Integer2 is range -(2**7) .. (2**7)-1; > > =A0 =A0for 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 > =A0 type Short_integer1 is range 0.. ((2 ** 8) - 1); > =A0 for Short_Integer1'Size use 8; > > =A0 subtype Short_integer2 is Integer range -(2 ** 7)..((2 ** 7) - 1); > > =A0 type Short_Integer3 is range -(2**7) .. (2**7)-1; > =A0 for Short_Integer3'Size use 8; > > =A0 subtype A_Float is Float; > > =A0 subtype A_Natural is Natural range 0..((2 ** 8) - 1); > =A0 -- The next line of code (if used) gives compilation error message: > =A0 -- A_NATURAL does not denote a first subtype > =A0 -- for A_Natural'Size use Short_integer1'Size; > > =A0 Size_Of_Float : integer :=3D Float'Size; =A0 =A0 =A0--32 bits long > > =A0 Size_Of_short_integer: integer :=3D Short_integer1'Size;--10 bits > long > =A0 Size_Of_short_int: integer :=3D Short_integer2'Size;--10 bits long > > =A0 Size_Of_Natural: integer :=3D A_Natural'Size; =A0 =A0 =A0 --8 bits lo= ng > > =A0 Arg =A0 =A0 : A_Float; > =A0 Result3 : Short_integer2; > =A0 Result2 : Short_integer3; > =A0 Result1 : Short_integer1; > > =A0 function Get_Bits2 is new Unchecked_Conversion (Source =3D> > =A0 =A0 Short_integer1, Target =3D> Short_integer3); > > Begin > > Arg :=3D 200.0; > > Result1 :=3D Short_integer1(Arg); =A0-- Result1 becomes 200 > Result2 :=3D Get_Bits2 (Result1); > Result3 :=3D 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 :=3D 200.0; Result : Signed_8 :=3D To_Signed_8 (Unsigned_8 (Arg)); begin Ada.Text_IO.Put (Signed_8'Image (Result)); end; -- Ludovic Brenta.