comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: 'Base
Date: Thu, 8 Dec 2005 20:57:49 -0600
Date: 2005-12-08T20:57:49-06:00	[thread overview]
Message-ID: <wu-dnXl3ud00bAXeRVn-qw@megapath.net> (raw)
In-Reply-To: 1134072443.635504.296550@g44g2000cwa.googlegroups.com

"Matthew Heaney" <mheaney@on2.com> wrote in message
news:1134072443.635504.296550@g44g2000cwa.googlegroups.com...
>
> Jeffrey R. Carter wrote:
> >
> > I'm surprised ObjectAda uses 32 bits for this. GNAT 3.4.2 uses 8 bits
(-128 .. 127).
>
> Exactly my point.  If you assume that you have more range than the RM
> promises, then you're only asking for trouble.

Not quite true. The range is always going to correspond to the
representation; so unless you have to worry about non-binary machines, you
can assume the next higher power of 2 minus 1; in this case 63. Of course,
usually this isn't particularly relevant - the extra values are a small
percentage of the total.

Moreover, if you only want to worry about typical machines, then the values
are only going to be some multiple of 8-bits, so 'Base'Last can pretty much
be assumed to be the smallest value of 2**(8*N-1)-1 without trouble.

But of course the whole point of these attributes is so you don't have to
assume at all.

BTW, 'Base works on any scalar type, and the rules for floats and fixed
point types are somewhat different than described in the messages here.

And a quick answer for the OP: using 'Base is usually only needed in various
tricks. Here's one. Imagine you want a type that can hold at least 0 ..
1000, but you don't care about the exact bounds and want the most efficient
type possible. You can declare:

     type Restricted_Type is range 0 .. 1000;
     type Big_Enough_Type is range Restricted_Type'Base'First ..
Restricted_Type'Base'Last;

and Big_Enough_Type will be some full-range type appropriate for the machine
(probably 16-bits or 32-bits).

Matt gave another example. Suppose you have a generic:
    generic
       type G_Int is range <>;
    package Gen is
        ...
and you need a counter of type G_Int that starts at zero and goes to the
upper bound of G_Int. Sloppy programmers would just use G_Int for such an
counter, but that would fail if the range of G_Int didn't include zero.
Slightly less sloppy programmers would declare the counter as having the
type G_Int'Base, but that isn't going to have the overflow check. The best
solution is to declare the counter as:
     Counter : G_Int'Base range 0 .. G_Int'Last;
which ensures that 0 is included, and the right range is used. Note that
this could cover a larger range than the original type.

                                  Randy.






  reply	other threads:[~2005-12-09  2:57 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-08 15:21 'Base ada_student
2005-12-08 18:08 ` 'Base Matthew Heaney
2005-12-08 18:44   ` 'Base Martin Dowie
2005-12-08 18:49     ` 'Base Martin Dowie
2005-12-08 19:24     ` 'Base Matthew Heaney
2005-12-08 20:27       ` 'Base Martin Dowie
2005-12-08 19:51     ` 'Base Jeffrey R. Carter
2005-12-08 20:07       ` 'Base Matthew Heaney
2005-12-09  2:57         ` Randy Brukardt [this message]
2005-12-09  2:13   ` Avoiding constraint checks w/ 'Base Anonymous Coward
2005-12-09  3:11     ` Randy Brukardt
2005-12-09 13:11   ` 'Base krischik
2005-12-09 13:52     ` 'Base Matthew Heaney
2005-12-09 20:42       ` 'Base Randy Brukardt
2005-12-08 19:11 ` 'Base Martin Krischik
2005-12-09 20:42   ` 'Base ada_student
2005-12-09 21:39     ` 'Base Pascal Obry
2005-12-10  3:30     ` 'Base Matthew Heaney
2005-12-10 14:50       ` 'Base ada_student
2005-12-10  7:52     ` 'Base Martin Krischik
2005-12-10 12:55       ` 'Base Larry Kilgallen
2005-12-10 13:37         ` 'Base Björn Persson
2005-12-11 11:00           ` 'Base Martin Krischik
2005-12-10 15:01         ` 'Base Robert A Duff
2005-12-11 10:59         ` 'Base Martin Krischik
2005-12-12  9:14       ` 'Base Ole-Hjalmar Kristensen
2005-12-12 19:08         ` 'Base Martin Krischik
2005-12-13 19:24           ` 'Base tmoran
2005-12-13 21:00         ` 'Base Georg Bauhaus
2005-12-14 19:43         ` 'Base Per Sandberg
2005-12-15 20:08           ` 'Base Martin Krischik
2005-12-16 19:19             ` 'Base Jeffrey R. Carter
2005-12-17  7:52               ` 'Base Martin Krischik
replies disabled

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