From: sjw <simon.j.wright@mac.com>
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)
Date: 2009-01-14T13:32:56-08:00 [thread overview]
Message-ID: <7ee787a3-3b79-451b-879c-17898bc541e2@r36g2000prf.googlegroups.com> (raw)
In-Reply-To: 139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com
On Jan 14, 8:13 pm, ChristopherL <clusard...@aol.com> wrote:
> On Jan 14, 8:08 am, Adam Beneschan <a...@irvine.com> wrote:
>
>
>
> > On Jan 14, 6:59 am, ChristopherL <clusard...@aol.com> 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
> > > subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );
> > > Result:Shrt;
>
> > > Arg:Float;
>
> > > function To_Bits is new
> > > Ada.Unchecked_Conversion (Source => Float, Target => Shrt);
>
> > > Begin
> > > Arg := 200.0;
>
> > > Result := To_Bits(Arg + 0.5); -- crude round then type conversion
> > > End test;
>
> > > -- Error Message Received
>
> > > 210 Ada.Unchecked_Conversion (Source => Float, Target =>
> > > Shrt);
>
> > > *** 387E-0: Identifier ADA is not directly visible
>
> > > 236 Result := To_Bits( Arg + 0.5 )
>
> > > *** 387E-0: Identifier TO_BITS is not directly visible
>
> > > ==================================================================
>
> > > -- Program (2) --
>
> > > with Unchecked_Conversion;
>
> > > procedure test is
> > > subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );
> > > Result:Shrt;
>
> > > Arg:Float;
>
> > > function To_Bits is new
> > > Unchecked_Conversion (Source => Float, Target => Shrt);
>
> > > Begin
> > > Arg := 200.0;
>
> > > Result := To_Bits(Arg + 0.5); -- crude round then type conversion
> > > End test;
>
> > (1) The error messages don't make any sense to me. If 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. You did include the "with" statement in your source
> > file, right, rather than putting it in another source? I'd check your
> > 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. I'm just guessing here. I have no idea what compiler you're
> > using.
>
> > (2) Even if you get past that, your program will not work as
> > intended. Unchecked_Conversion simply copies bit representations from
> > one type to another. A floating-point type will contain bits in a
> > floating-point format, and that format won't look like an integer
> > format. If Float is an IEEE-standard 32-bit float, the representation
> > of 200.0 looks like
>
> > 01000011010010000000000000000000
>
> > 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. But most likely you'll either get the
> > 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. Did I guess right? If that's the case, it might
> > be helpful to do this the simple way:
>
> > type Signed_Short is range -128 .. 127;
> > -- more readable than "Shrt", which could be mistaken for
> > -- "shirt", or some other word with four letters
> > for Signed_Short'Size use 8;
>
> > type Unsigned_Short is range 0 .. 255;
> > for Unsigned_Short'Size use 8;
>
> > Arg : Float;
> > Unsigned_Result : Unsigned_Short;
> > Result : Signed_Short;
> > begin
> > Arg := 200.0;
> > Unsigned_Result := Unsigned_Short (Arg + 0.5);
> > -- will raise exception if Arg is out of the range 0.0 ..
> > 255.0,
> > -- after rounding
>
> > if Unsigned_Result >= 128
> > then Result := Signed_Short (Integer(Unsigned_Result) - 256)
> > else Result := Signed_Short (Unsigned_Result);
> > end 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:
>
> > if Arg >= 128.0
> > then Result := Signed_Short (Arg + 0.5 - 256.0)
> > else Result := Signed_Short (Arg + 0.5);
> > end if;
>
> > Or you can use Unchecked_Conversion if you want:
>
> > function To_Signed_Short is new Unchecked_Conversion
> > (Source => Unsigned_Short, Target => Signed_Short);
> > ...
> > begin
> > ...
> > Unsigned_Result := Unsigned_Short (Arg + 0.5);
> > Result := To_Signed_Short (Unsigned_Result);
>
> > This seems to be what you want... copying bits from an unsigned
> > integer to a signed integer. Not 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; -- 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 = 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.
next prev parent reply other threads:[~2009-01-14 21:32 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com>
2009-01-14 1:33 ` How to put 200 into an integer sub-type of 16 bits (code included) Brian Drummond
[not found] ` <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com>
2009-01-14 9:06 ` Ludovic Brenta
2009-01-14 10:08 ` Georg Bauhaus
2009-01-14 10:29 ` Georg Bauhaus
2009-01-14 12:47 ` Brian Drummond
2009-01-14 12:53 ` Brian Drummond
[not found] ` <f4894476-851e-493f-93a2-168976bd97fb@s1g2000prg.googlegroups.com>
2009-01-14 16:08 ` Adam Beneschan
2009-01-14 21:17 ` sjw
[not found] ` <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com>
2009-01-14 20:41 ` Adam Beneschan
[not found] ` <c265ffb7-6159-4d85-b259-78b830e115f9@v18g2000pro.googlegroups.com>
2009-01-15 2:39 ` SteveD@teranews.com
[not found] ` <3625f980-4406-4f51-b494-dd4a48cab840@p36g2000prp.googlegroups.com>
2009-01-15 6:53 ` Michael Bode
2009-01-15 8:57 ` Martin
[not found] ` <fc154f52-7168-447a-bcd3-6ece9066ebf7@r37g2000prr.googlegroups.com>
2009-01-15 10:29 ` Georg Bauhaus
[not found] ` <1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com>
2009-01-14 22:47 ` Adam Beneschan
2009-01-14 23:11 ` Ludovic Brenta
2009-01-15 9:56 ` Stuart
2009-01-15 12:40 ` Stuart
2009-01-15 14:02 ` Stephen Leake
2009-01-15 15:54 ` Dmitry A. Kazakov
2009-01-15 16:29 ` Hyman Rosen
2009-01-15 22:15 ` Dmitry A. Kazakov
2009-01-16 9:11 ` Jacob Sparre Andersen
2009-01-16 11:03 ` Mike H
2009-01-16 22:16 ` Brian Drummond
2009-01-14 21:32 ` sjw [this message]
2009-01-14 21:51 ` Brian Drummond
2009-01-14 18:49 ` Jeffrey R. Carter
2009-01-15 10:12 ` Quote of the Day (Re: " Peter Hermann
2009-01-14 21:09 ` sjw
2009-01-14 21:16 ` Adam Beneschan
2009-01-14 23:09 ` Martin
2009-01-15 0:07 ` Adam Beneschan
2009-01-15 3:09 ` Randy Brukardt
2009-01-15 16:28 ` Adam Beneschan
2009-01-15 21:21 ` Robert A Duff
2009-01-16 1:17 ` Adam Beneschan
2009-01-16 14:55 ` Robert A Duff
2009-01-15 14:44 ` Brian Drummond
[not found] ` <3f1f2f67-5d69-4baf-8e8c-0d2b5f68475f@p36g2000prp.googlegroups.com>
2009-01-15 16:36 ` How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included) Adam Beneschan
[not found] ` <8e64f509-f6fe-4d86-ae1a-fe0b1c88555a@v5g2000pre.googlegroups.com>
2009-01-15 17:09 ` christoph.grein
2009-01-15 17:17 ` Adam Beneschan
2009-01-15 17:29 ` Georg Bauhaus
[not found] ` <09b4a056-688d-49c8-b935-fa0b30f1ae84@w1g2000prk.googlegroups.com>
2009-01-15 18:40 ` Mike H
[not found] ` <be26729d-9458-42fa-8c8c-004ca33b790d@f33g2000vbf.googlegroups.com>
2009-01-15 20:57 ` Niklas Holsti
[not found] ` <97231951-54a0-4df7-bb73-04261b34287f@e6g2000vbe.googlegroups.com>
2009-01-16 3:17 ` Steve D
[not found] ` <a61abb30-bc60-4d13-b298-f369ddc8f741@z6g2000pre.googlegroups.com>
2009-01-15 18:15 ` Martin
2009-01-15 18:34 ` Stuart
2009-01-15 17:23 ` Georg Bauhaus
[not found] ` <d3b7ad53-af51-4ac5-9167-7cb99e61b2b1@v5g2000pre.googlegroups.com>
2009-01-15 17:50 ` Stuart
[not found] <6c7964a6-1733-434b-b1b1-962baa4ebba2@p23g2000prp.googlegroups.com>
2009-01-13 21:01 ` How to put 200 into an integer sub-type of 16 bits (code included) Ludovic Brenta
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox