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,FROM_NUMERIC_TLD autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,30e0ceaf4e6be70c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-07-04 01:40:07 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!nntp.abs.net!lore.csc.com!baen1673807.greenlnk.net!baen1673807!not-for-mail From: Stuart Palin Newsgroups: comp.lang.ada Subject: Re: Simple program to find average of 3 numbers Date: Fri, 04 Jul 2003 09:37:45 +0100 Organization: Computer Sciences Corporation Message-ID: <3F053CD9.135E7340@0.0> References: <3F0511BF.6000403@attbi.com> NNTP-Posting-Host: 20.44.240.3 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: lore.csc.com 1057307866 24219 20.44.240.3 (4 Jul 2003 08:37:46 GMT) X-Complaints-To: abuse@news.csc.com NNTP-Posting-Date: Fri, 4 Jul 2003 08:37:46 +0000 (UTC) X-Mailer: Mozilla 4.5 [en] (WinNT; I) X-Accept-Language: en X-Original-NNTP-Posting-Host: rc2966.rochstr.gmav.gecm.com X-Original-Trace: 4 Jul 2003 09:35:13 +0100, rc2966.rochstr.gmav.gecm.com Xref: archiver1.google.com comp.lang.ada:40047 Date: 2003-07-04T09:37:45+01:00 List-Id: "Robert I. Eachus" wrote: > > prashna wrote: > > Hi friends, > > > > What is wrong in the following program which is giving constraint > > error? > > > > procedure average is type ONE_TO_100 is range 1 .. 100; > > > > OBJ1, OBJ2, OBJ3, AVG : ONE_TO_100; begin OBJ1 := 70; OBJ2 := 70; > > OBJ3 := 70; AVG := OBJ1+OBJ2+OBJ3/3; end average; > > > Oh, and then I could talk about your formatting, and identifier style. > But I will address those by example. Try: > > procedure Average is > subtype One_to_100 is Integer range 1 .. 100; > Obj_1, Obj_2, Obj_3: One_to_100 := 70; > Avg: One_to_100; > begin > Avg := (Obj_1+Obj_2+Obj_3)/3; > -- I assume you really wanted the average, rather than 163. > end Average; You might also want to consider what values the intermediate calculation Obj_1+Obj_2+Obj_3 can take. In this instance because your type is constrained to a number which should be well within the bounds of integer you would not experience a problem. Questions to ask yourself (and seek answers to): a) What if the bounds of the type were larger? b) How might it be possible to detect a potential constraint error at compile time rather than during execution? -- Stuart Palin