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 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-7-bit Path: g2news1.google.com!postnews.google.com!z27g2000prd.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: How to put 200 into an integer sub-type of 16 bits (code included) Date: Tue, 13 Jan 2009 13:01:51 -0800 (PST) Organization: http://groups.google.com Message-ID: <62451c87-a63a-4ba5-9bf7-90439b9e4e2b@z27g2000prd.googlegroups.com> References: <6c7964a6-1733-434b-b1b1-962baa4ebba2@p23g2000prp.googlegroups.com> NNTP-Posting-Host: 94.108.242.169 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1231880511 8084 127.0.0.1 (13 Jan 2009 21:01:51 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 13 Jan 2009 21:01:51 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: z27g2000prd.googlegroups.com; posting-host=94.108.242.169; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.18) Gecko/20081030 Iceape/1.1.13 (Debian-1.1.13-1),gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:3273 Date: 2009-01-13T13:01:51-08:00 List-Id: ChristopherL wrote on comp.lang.ada: > (1)The below code compiles but I get a run time exception. > > If I do not modify the definitions of Arg or Result how can I > make Arg's value fit into Result's 16 bits. > > Result is a subrange of integer going from -128 to 127 which is > 16 bits. > > Arg has more than 16 bits. > > -- Code -- > > procedure test is > subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 ); Since you don't specify the size of this type, there is no guarantee that it is 16 bits. It may as well be 32 bits which is the default for Integer on most compilers. But that's a detail; what matters is the range you specified. > Result:Shrt; > > Arg:Float; > > Begin > Arg := 200.0; > > Result := Shrt(Arg + 0.5); -- type conversion > End test; > > -- Run Time Error -- > Unhandled exception: > Constraint Error raised in Main > range check failed You get this constraint_error because the value 200 is not in the range -128..127. Surprised? > ========================== > (2) The following code will put 201 in test when I want it to be 200, > and I get the same error message. > > -- Code -- > > procedure test is > type Unsigned_short is range 0..(2**8)-1; > temp: Unsigned_short; > Begin > Arg := 200.0; > temp := Unsigned_short( Arg + 0.5 ); > > Result := Shrt(temp); -- type conversion > End test; > > -- Run Time Error -- > Unhandled exception: > Constraint Error raised in Main > range check failed > > ========================== > > How can I put this 200 into Result. You can't. But I suspect you're not asking the right questions. Maybe a better question would be: since my requirement is to hold the value 200 in an integer variable, what range should that variable be? More generally, what values is my variable required to support? > Besides Arg and Result, can I do it without defining any more types > or variables. You probably can, but since you have not stated what your problem is (you only explained what your non-working solutions were), there is little anyone can do to help. Your best option is to explain what you are trying to achieve. -- Ludovic Brenta.