comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@attbi.com>
Subject: Re: Simple program to find average of 3 numbers
Date: Wed, 09 Jul 2003 21:38:43 GMT
Date: 2003-07-09T21:38:43+00:00	[thread overview]
Message-ID: <3F0C8B45.2080108@attbi.com> (raw)
In-Reply-To: 1ec946d1.0307090725.73f5f200@posting.google.com

Matthew Heaney wrote:

 > This is actually an important question, because it highlights some
 > subtleties in the Ada type system.
 >
 > In the type declaration
 >
 > type T is range 1 .. 100;
 >
 > the type T is actually a subtype of the underlying type.  The
 > underlying type has no name, but we can refer to it as T'Base.  The
 > name T is its "first named subtype."
 >
 > The type has a range that includes all the values in the first named
 > subtype T, and is symmetric around 0.

AT LEAST all the values in the range, and is symmetric around zero with
a possible extra negative value.  (Can you tell I am a language lawyer
by nature?)

If you want to ensure a precise range for the base type, specify the
size as well:

type Tiny_Int is range -128..127;
for Tiny_Int'Size use 8;

 > The addition is computed using the range of values in the base type.

No,  "The predefined operations on integer types either yield the
mathematically correct result or raise the exception Constraint_Error."
RM 4.5(10)  Arithmetic operations are not required to raise
Constraint_Error if the value of the expression as a whole is in range,
because that would prevent many useful optimizations.  (However, the
sentence above was moved from 11.6 in Ada 83 to 4.5.  But the meaning
hasn't changed.)  For example, if you write Z := 2*X + 2*Y; the compiler
is allowed to generate code for 2*(X+Y).  This will only generate an
exception in cases where 2*X + 2*Y does, but there are some cases where
you get the correct mathmatical result with the optimized expression,
and a Constraint_Error with the unoptimized version.

 > In particular, this means that the programmer is responsible for
 > ensuring that the base range of the type includes a wide enough range
 >  to compute intermediate values during expression evalution...

Correct.

 > The type declaration in your example looks like this:
 >
 > type T is new Integer range 1 .. 100;
 >
 > However, I'm not sure whether it corresponds to the declarations
 >
 > type T_Base is new Integer; subtype T is T_Base range 1 .. 100;
 >
 > or to the declarations:
 >
 > type T_Base is range -100 .. 100; subtype T is T_Base range 1 .. 100;
 >
 >
 > It executes fine when I run it, so it seems to correspond to the
 > former pair.

It corresponds to:

     subtype T is <implementation defined integer type> range 1..100;

except that one thing we learned from Ada 83 is that such "helpful"
analogies are always wrong. ;-)  Which is why you won't find any in the
Ada 95 RM.

If you want to find out what the compiler will do you can write a short
program to find out:
--------------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
procedure Num_Types is

   type One_To_100 is range 1..100;

   type Eight_Bits is range 1..100;
   for Eight_Bits'Size use 8;

begin

New_Line;
Put_Line(" Type One_To_100 is " &
Integer'Image(Integer(One_To_100'Base'First))
   & ".." & Integer'Image(Integer(One_To_100'Base'Last)) & '.');
Put_Line(" Type Eight_Bits is " &
Integer'Image(Integer(Eight_Bits'Base'First))
   & ".." & Integer'Image(Integer(Eight_Bits'Base'Last)) & '.');
New_Line;

end Num_Types;
------------------------------------------------------------------------
with GNAT:

num_types

  Type One_To_100 is -128.. 127.
  Type Eight_Bits is -128.. 127.

If I seem to be picking on Matt, I'm not.  His answer was close enough
to right that I wanted to be sure to correct the spots where it wasn't
perfect.  This is an area where the changes in the rules between Ada 83 
and Ada 95 can be confusing, even though for the most part they say 
exactly the same thing.  (The differences are very subtle, involving 
what you can count on being correct after Constraint_Error is raised.)

-- 

                                              Robert I. Eachus

�In an ally, considerations of house, clan, planet, race are
insignificant beside two prime questions, which are: 1. Can he shoot? 2.
Will he aim at your enemy?� -- from the Laiden novels by Sharon Lee and
Steve Miller.




  reply	other threads:[~2003-07-09 21:38 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-04  5:01 Simple program to find average of 3 numbers prashna
2003-07-04  5:22 ` John R. Strohm
2003-07-04  5:34   ` Cephus�
2003-07-04  9:54     ` Tarjei T. Jensen
2003-07-05 17:49       ` Cephus�
2003-07-04  5:33 ` Robert I. Eachus
2003-07-04  8:37   ` Stuart Palin
2003-07-05 10:06     ` Preben Randhol
2003-07-04  5:39 ` tmoran
2003-07-04  9:28   ` prashna
2003-07-04 11:02     ` Stuart Palin
2003-07-04 16:20     ` Pascal Obry
2003-07-05  1:25       ` Robert I. Eachus
     [not found]     ` <1e3ht-ig4.ln1@beastie.ix.netcom.com>
2003-07-04 21:17       ` Jeffrey Creem
2003-07-05  5:07         ` Anders Wirzenius
2003-07-05 14:39 ` Marqmc5
2003-07-06  3:47 ` g_ak
2003-07-08  5:06   ` prashna
2003-07-09 15:25     ` Matthew Heaney
2003-07-09 21:38       ` Robert I. Eachus [this message]
2003-07-10 13:52         ` Matthew Heaney
2003-07-10 15:46           ` Robert I. Eachus
2003-07-11  0:56             ` Randy Brukardt
2003-07-11 21:54           ` Robert A Duff
2003-07-11 21:50         ` Robert A Duff
2003-07-11 23:32           ` Robert I. Eachus
2003-07-10  4:55       ` Simon Wright
  -- strict thread matches above, loose matches on Subject: below --
2003-07-09  6:24 christoph.grein
replies disabled

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