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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f9957894e0bdf128 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!r36g2000prf.googlegroups.com!not-for-mail From: sjw Newsgroups: comp.lang.ada Subject: Re: How to put 200 into an integer sub-type of 16 bits (code included) Date: Wed, 14 Jan 2009 13:32:56 -0800 (PST) Organization: http://groups.google.com Message-ID: <7ee787a3-3b79-451b-879c-17898bc541e2@r36g2000prf.googlegroups.com> References: <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com> <71gqm49eatq868htrvd7eghm3m8su8kcbl@4ax.com> <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com> <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com> NNTP-Posting-Host: 62.49.19.209 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1231968776 20281 127.0.0.1 (14 Jan 2009 21:32:56 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 14 Jan 2009 21:32:56 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: r36g2000prf.googlegroups.com; posting-host=62.49.19.209; posting-account=_RXWmAoAAADQS3ojtLFDmTNJCT0N2R4U User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:3301 Date: 2009-01-14T13:32:56-08:00 List-Id: On Jan 14, 8:13=A0pm, ChristopherL wrote: > On Jan 14, 8:08=A0am, Adam Beneschan wrote: > > > > > On Jan 14, 6:59 am, ChristopherL wrote: > > > > Hello again, > > > > Trying twice, I seem not to be able to get the "Unchecked_Conversion" > > > to compile. See below, and explain this to me. > > > > Thanks, > > > Chris L. > > > > -- Program (1) -- > > > > with Ada.Unchecked_Conversion; > > > > procedure test is > > > =A0 =A0subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 ); > > > =A0 =A0Result:Shrt; > > > > =A0 =A0Arg:Float; > > > > =A0 =A0function To_Bits is new > > > =A0 =A0 =A0 Ada.Unchecked_Conversion (Source =3D> Float, Target =3D> = Shrt); > > > > Begin > > > =A0 =A0Arg :=3D 200.0; > > > > =A0 =A0Result :=3D To_Bits(Arg + 0.5); =A0-- crude round then type co= nversion > > > End test; > > > > -- Error Message Received > > > > =A0 =A0 210 =A0 =A0 =A0 Ada.Unchecked_Conversion (Source =3D> Float, = Target =3D> > > > Shrt); > > > > *** 387E-0: Identifier ADA is not directly visible > > > > =A0 =A0 236 =A0 =A0 =A0 Result :=3D To_Bits( Arg + 0.5 ) > > > > *** 387E-0: Identifier TO_BITS is not directly visible > > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > > -- Program (2) -- > > > > with Unchecked_Conversion; > > > > procedure test is > > > =A0 =A0subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 ); > > > =A0 =A0Result:Shrt; > > > > =A0 =A0Arg:Float; > > > > =A0 =A0function To_Bits is new > > > =A0 =A0 =A0 Unchecked_Conversion (Source =3D> Float, Target =3D> Shrt= ); > > > > Begin > > > =A0 =A0Arg :=3D 200.0; > > > > =A0 =A0Result :=3D To_Bits(Arg + 0.5); =A0-- crude round then type co= nversion > > > End test; > > > (1) The error messages don't make any sense to me. =A0If you say "with > > Ada.Unchecked_Conversion", then Ada and Ada.Unchecked_Conversion > > should both be visible and you shouldn't be getting "not directly > > visible" errors. =A0You did include the "with" statement in your source > > file, right, rather than putting it in another source? =A0I'd check you= r > > compiler manual here; maybe you didn't configure things properly and > > you need to do something special in order to access the Ada standard > > library. =A0I'm just guessing here. =A0I have no idea what compiler you= 're > > using. > > > (2) Even if you get past that, your program will not work as > > intended. =A0Unchecked_Conversion simply copies bit representations fro= m > > one type to another. =A0A floating-point type will contain bits in a > > floating-point format, and that format won't look like an integer > > format. =A0If Float is an IEEE-standard 32-bit float, the representatio= n > > of 200.0 looks like > > > =A0 =A001000011010010000000000000000000 > > > If you convert this to Shrt, which is probably 8 bits, the language > > doesn't really tell you what happens when you Unchecked_Conversion a > > 32-bit value to an 8-bit value. =A0But most likely you'll either get th= e > > upper 8 bits (01000011) or the lower 8 bits (00000000), neither of > > which is what you want. > > > You need a regular type conversion to convert the float to an integer, > > not an Unchecked_Conversion. > > > (3) You still haven't been clear on what you want to do, but it now > > looks like you're trying to take an 8-bit integer in the range 0..255 > > and convert it to an 8-bit Shrt (in the range -128..127) with the same > > bit representation. =A0Did I guess right? =A0If that's the case, it mig= ht > > be helpful to do this the simple way: > > > =A0 =A0type Signed_Short is range -128 .. 127; > > =A0 =A0 =A0-- more readable than "Shrt", which could be mistaken for > > =A0 =A0 =A0-- "shirt", or some other word with four letters > > =A0 =A0for Signed_Short'Size use 8; > > > =A0 =A0type Unsigned_Short is range 0 .. 255; > > =A0 =A0for Unsigned_Short'Size use 8; > > > =A0 =A0Arg : Float; > > =A0 =A0Unsigned_Result : Unsigned_Short; > > =A0 =A0Result : Signed_Short; > > begin > > =A0 =A0Arg :=3D 200.0; > > =A0 =A0Unsigned_Result :=3D Unsigned_Short (Arg + 0.5); > > =A0 =A0 =A0 =A0-- will raise exception if Arg is out of the range 0.0 .= . > > 255.0, > > =A0 =A0 =A0 =A0-- after rounding > > > =A0 =A0if Unsigned_Result >=3D 128 > > =A0 =A0 =A0 then Result :=3D Signed_Short (Integer(Unsigned_Result) - 2= 56) > > =A0 =A0 =A0 else Result :=3D Signed_Short (Unsigned_Result); > > =A0 =A0end if; > > > (You may have to convert to "Integer" before subtracting 256 because > > otherwise Ada may try to use 8-bit integers to do the arithmetic and > > then > > things will be out of range.) > > > Or maybe you don't need the Unsigned_Short type: > > > =A0 =A0if Arg >=3D 128.0 > > =A0 =A0 =A0 then Result :=3D Signed_Short (Arg + 0.5 - 256.0) > > =A0 =A0 =A0 else Result :=3D Signed_Short (Arg + 0.5); > > =A0 =A0end if; > > > Or you can use Unchecked_Conversion if you want: > > > =A0 =A0function To_Signed_Short is new Unchecked_Conversion > > =A0 =A0 =A0 =A0(Source =3D> Unsigned_Short, Target =3D> Signed_Short); > > =A0 =A0... > > begin > > =A0 =A0... > > =A0 =A0Unsigned_Result :=3D Unsigned_Short (Arg + 0.5); > > =A0 =A0Result :=3D To_Signed_Short (Unsigned_Result); > > > This seems to be what you want... copying bits from an unsigned > > integer to a signed integer. =A0Not copying bits from a float to an > > integer---that is not at all what you're trying to accomplish. > > I want to copy bits from an Ada float to a user defined short integer > maintaining the same > bit representation. > > Arg:Float; =A0-- From this with a value of 200 > subtype Short_integer is Integer range -(2 ** 7) .. ((2 ** 7) - 1 ); > Result: Short_integer; --to this, and I want to round Arg Chris, Never mind the rounding; tell us what you need the value in Result to be for Arg =3D 200? It clearly can't be 200, because that's outside the range you've specified. Note that the numeric range you've specified for Short_Integer will fit into 8 bits, but because you've made it a subtype of Integer, variables will occupy the same space as an Integer (32 bits usually; it will be at least 16 bits). It'd also help to know what compiler you're using.