comp.lang.ada
 help / color / mirror / Atom feed
From: "Stuart" <stuart@0.0>
Subject: Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion
Date: Fri, 16 Jan 2009 12:18:34 -0000
Date: 2009-01-16T12:18:34+00:00	[thread overview]
Message-ID: <497076b5$1_1@glkas0286.greenlnk.net> (raw)
In-Reply-To: 83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com

"ChristopherL" <clusardi2k@aol.com> wrote in message 
news:83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com...

> 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!

<snip problem with Unchecked_Conversion>

> I want to copy bits from an Ada float to a user defined short integer
> maintaining the same bit representation.

As said many times before this is a very unclear statement of what you are 
really trying to do and it is resulting in a lot of confusion.  You really 
need to read through the replies in your previous thread - especially as you 
have re-introduced the confusion of bits and values again.

<snip code>

I will have another guess at what you might be trying to do and provide a 
solution:

procedure ada_main is
   -- Things the OP says can't be changed:
   -- The definitions of Arg and Result2 can not chanage.
  subtype A_Float is Float;
  subtype Short_integer2 is Integer range -(2 ** 7)..((2 ** 7) - 1);

  Arg     : A_Float;
  Result2 : Short_integer2;

  -- Some things the OP has still not made clear:
  -- 1) What he is really trying to achieve.
  -- 2) Whether he wants to copy bits or values.
  -- 3) How he thinks values of Arg greater than 127.0
  --    ought to be represented in an integer type with
  --    a range -128..127
  -- 4) How rounding should take place (round up, round down,
  --    round nearest)

  -- Because of OP insistence that the the definitions cannot be changed
  -- and the fact Short_integer2 is based on integer which is implementation
  -- defined using Unchecked_Conversion here would be even more dubious
  -- than usual.

  -- Assuming he is expecting whole number values in Arg and
  -- wants a value in Result2 that is unique over the range
  -- 0.0 .. 200.0 and is easily relatable to the original
  -- value of Arg.

  -- Declare a sub-type capable of supporting all values of Arg and
  -- the computations we want to do:
   subtype S is Short_integer2'base range -256..200;
     -- REQUIREMENT:  The lower bound of S must be such that
     -- Short_integer2'first-Short_integer'last-1 <= S'first <=
     -- -(max(Arg) + 1)
     -- The upper bound must be such that
     -- S'last >= max(Arg)

     -- NB the lower bound is chosen to control the underlying
     -- representation when Arg > Short_integer2'last

     -- Assumes the base type of Short_integer2 will allow this;
     -- compiler will complain if not.

     -- If the base type does not support the range, a suitable
     -- new type to do the integer based calculations will be needed
     -- and some explicit type conversions will have to be made.

   -- I can't be bothered to think of a meaningful name for the subtype!!

   Int_Arg : S; -- Holds the integer value of Arg.

   V : Short_integer2;
   pragma volatile(V);
      -- Just to force demonstration code to do something external
begin

   Arg := 200.0;  -- OP chosen test case!

   Int_Arg := S(Arg);
      -- Convert to an integer value using Ada default rounding.

   -- Resolve cases where the value is outside the range of Short_integer2
   if (Int_Arg > Short_integer2'last) then
      Result2 := Int_Arg + S'first;
      -- Given the user specified constraints on Arg and the definition
      -- of Short_integer2 this will not exceed constraints.
   else
      Result2 := Int_Arg;
   end if;

   V := Result2;
end ada_main;

-- 
Stuart 





  parent reply	other threads:[~2009-01-16 12:18 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
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 [this message]
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