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-Language: ENGLISH,CP1252 X-Google-Thread: 103376,30e0ceaf4e6be70c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-07-09 14:38:44 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!in.100proofnews.com!in.100proofnews.com!cycny01.gnilink.net!cyclone1.gnilink.net!wn14feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!rwcrnsc52.ops.asp.att.net.POSTED!not-for-mail Message-ID: <3F0C8B45.2080108@attbi.com> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Simple program to find average of 3 numbers References: <6ZMNa.109291$0B.2183354@wagner.videotron.net> <1ec946d1.0307090725.73f5f200@posting.google.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit NNTP-Posting-Host: 24.62.164.137 X-Complaints-To: abuse@comcast.net X-Trace: rwcrnsc52.ops.asp.att.net 1057786723 24.62.164.137 (Wed, 09 Jul 2003 21:38:43 GMT) NNTP-Posting-Date: Wed, 09 Jul 2003 21:38:43 GMT Organization: Comcast Online Date: Wed, 09 Jul 2003 21:38:43 GMT Xref: archiver1.google.com comp.lang.ada:40153 Date: 2003-07-09T21:38:43+00:00 List-Id: 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 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.