comp.lang.ada
 help / color / mirror / Atom feed
From: Martin <martin.dowie@btopenworld.com>
Subject: Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion
Date: Thu, 15 Jan 2009 13:24:04 -0800 (PST)
Date: 2009-01-15T13:24:04-08:00	[thread overview]
Message-ID: <6540d68e-2cc7-45aa-a5d2-ac16fa335822@i24g2000prf.googlegroups.com> (raw)

On Jan 15, 8:53 pm, ChristopherL <clusard...@aol.com> wrote:
> I'm restarting the tread [How to put 200 into an integer sub-type of
> 16 bits (code included)] with a new subject because the last was too
> long!
>
> How can I be getting the below error message with
> "Unchecked_Conversion".
>
> Isn't Unchecked_conversion just suppose to copy bits?
>
> 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 );
> Result2: Short_integer; --to this, and I want to round Arg
>
> These 3 definitions of Arg and Result2 can not chanage.
>
> Arg will never be negative or greater than 200/201.
>
> Chris L.
>
> with Unchecked_Conversion;
>
> procedure test is
>   subtype Short_integer1 is Natural range 0.. ((2 ** 10) - 1);
>   subtype Short_integer2 is Integer range -(2 ** 7)..((2 ** 7) - 1);
>
>   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

Are you sure about those sizes? Try:

Put_Line ("Float'Size          =" & Integer'Image (Float'Size));
Put_Line ("Short_integer1'Size =" & Integer'Image
(Short_integer1'Size));
Put_Line ("Short_integer2'Size =" & Integer'Image
(Short_integer2'Size));
Put_Line ("Natural'Size        =" & Integer'Image (Natural'Size));

An aside - probably wasted if you don't understand subtypes and ranges
- but you don't have to assign everything to 'integer'. Instead you
can store the constants like this:

   Size_Of_Float : constant := Float'Size;

The benefit is that you can mix this into just about any expression
without having to bother about type conversions.

> Result2 := Get_Bits (Result1);

Result2 is of type Short_integer2 - which _you_ defined to be of the
range -128 to 127 (inclusive). Why are you surprised that 201 does
fit??? And that's assuming the Unchecked_Conversion works - double
check the actual sizes!

Cheers
-- Martin



             reply	other threads:[~2009-01-15 21:24 UTC|newest]

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

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