comp.lang.ada
 help / color / mirror / Atom feed
From: Adrian Hoe <abyhoe@gmail.com>
Subject: Re: What is the best way to convert Integer to Short_Short_Integer?
Date: Thu, 10 Jun 2010 21:26:46 -0700 (PDT)
Date: 2010-06-10T21:26:46-07:00	[thread overview]
Message-ID: <e4ee8519-9b92-4b50-91eb-169cdb4b00c8@q39g2000prh.googlegroups.com> (raw)
In-Reply-To: hus9pf$er2$1@tornado.tornevall.net

On Jun 11, 11:14 am, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> wrote:
> You convert between numeric types by using a type conversion:
>
> I : Integer := Integer'Last;
> S : Short_Short_Integer; -- Not portable
> ...
> S := Short_Short_Integer (I);
>
> If the value of I is not within the range of Short_Short_Integer, the conversion
> will raise Constraint_Error.
>
> This is basic Ada, and if it's not what you want, then you should use another
> term than "convert" (and a lengthy explanation of what you're trying to achieve
> 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



  reply	other threads:[~2010-06-11  4:26 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-11  1:17 What is the best way to convert Integer to Short_Short_Integer? Adrian Hoe
2010-06-11  2:21 ` Adrian Hoe
2010-06-11  3:14 ` Jeffrey R. Carter
2010-06-11  4:26   ` Adrian Hoe [this message]
2010-06-11  7:07     ` Jeffrey R. Carter
2010-06-11  8:59       ` J-P. Rosen
2010-06-11 12:28       ` Maciej Sobczak
2010-06-11 19:05         ` Randy Brukardt
2010-06-13  4:26         ` BrianG
2010-06-13 18:07           ` Maciej Sobczak
2010-06-14  7:28             ` Georg Bauhaus
2010-06-14 16:45             ` Keith Thompson
2010-06-15  4:54               ` Martin Krischik
2010-06-22 21:48                 ` Keith Thompson
2010-06-15  2:24             ` BrianG
2010-06-15 21:21               ` Maciej Sobczak
2010-06-15 23:39                 ` Georg Bauhaus
2010-06-16  1:27                 ` BrianG
2010-06-11 12:31       ` Brian Drummond
replies disabled

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