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=-0.9 required=5.0 tests=BAYES_00,FROM_NUMERIC_TLD autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8af7fe40dc0a55ae X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!news-peer-lilac.gradwell.net!not-for-mail From: "Stuart" Newsgroups: comp.lang.ada References: <83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com> Subject: Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion Date: Fri, 16 Jan 2009 12:18:34 -0000 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350 Message-ID: <497076b5$1_1@glkas0286.greenlnk.net> X-Original-NNTP-Posting-Host: glkas0286.greenlnk.net NNTP-Posting-Host: 20.133.0.1 X-Trace: 1232108324 news.gradwell.net 505 dnews/20.133.0.1:21547 X-Complaints-To: news-abuse@gradwell.net Xref: g2news1.google.com comp.lang.ada:3380 Date: 2009-01-16T12:18:34+00:00 List-Id: "ChristopherL" 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! > 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. 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