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,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,e1bb40a3d604c4b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!q39g2000prh.googlegroups.com!not-for-mail From: Adrian Hoe Newsgroups: comp.lang.ada Subject: Re: What is the best way to convert Integer to Short_Short_Integer? Date: Thu, 10 Jun 2010 21:26:46 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: 60.53.27.161 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1276230406 22634 127.0.0.1 (11 Jun 2010 04:26:46 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 11 Jun 2010 04:26:46 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: q39g2000prh.googlegroups.com; posting-host=60.53.27.161; posting-account=coq9PAkAAAB2Xx46RZLFJw5dY9DVXW4- User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/531.22.7 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7,gzip(gfe) Xref: g2news1.google.com comp.lang.ada:11627 Date: 2010-06-10T21:26:46-07:00 List-Id: On Jun 11, 11:14=A0am, "Jeffrey R. Carter" wrote: > You convert between numeric types by using a type conversion: > > I : Integer :=3D Integer'Last; > S : Short_Short_Integer; -- Not portable > ... > S :=3D Short_Short_Integer (I); > > If the value of I is not within the range of Short_Short_Integer, the con= version > will raise Constraint_Error. > > This is basic Ada, and if it's not what you want, then you should use ano= ther > term than "convert" (and a lengthy explanation of what you're trying to a= chieve > and why is probably in order). > > -- > Jeff Carter > "In the frozen land of Nador they were forced to > eat Robin's minstrels, and there was much rejoicing." > Monty Python & the Holy Grail > 70 Sorry for being vague. I have a number which is in the range of -128..127 and is read as Integer (32-bit). The reason that I want to convert to Short_Short_Integer is to save 24 bits of every such value stored in a record type and to enforce its range on user input. The if..else statement performs range checking during conversion. As long as the range is checked, of course, we can use either method: type conversion as Jeff suggested or use Unchecked_Conversion. Range_Error is a programmer defined exception and could be redundant since Ada has already had Constraint_Error. The main purpose of Range_Error is there to differentiate an exception raised from the code or Constraint_Error from within the Ada runtime range checking mechanism. In the event of Range_Error, we will know a value has fallen out of a defined range and an exception is raised from a procedure or function. Which is the preferred method? Short_Short_Integer (I) or Unchecked_Conversion? And why Jeff said Short_Short_Integer not portable? Is the implementation of Short_Short_Integer machine dependent? If Short_Short_Integer is not portable, what is the best implementation in your opinion? Convert to a smaller integer to save mere 24 bits or keep it as 32-bit Integer and perform range checking? In another similar but different scenario, where a value is in the range 0..15 and is read as 32-bit Integer. Should I convert to Unsigned_4? Where: type Unsigned_4 is mod 2**4; for Unsigned_4'Size use 4; Thanks. -- Adrian Hoe