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-Thread: 103376,fc8384b47d495708 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!peer01.cox.net!peer02.cox.net!cox.net!news-server.columbus.rr.com!fe2.columbus.rr.com.POSTED!not-for-mail From: "John B. Matthews" Newsgroups: comp.lang.ada Subject: Re: Should be getting a constraint error in this code References: User-Agent: MT-NewsWatcher/3.3b1 (PPC Mac OS X) Message-ID: Date: Sat, 24 Jul 2004 03:35:43 GMT NNTP-Posting-Host: 65.31.62.213 X-Complaints-To: abuse@rr.com X-Trace: fe2.columbus.rr.com 1090640143 65.31.62.213 (Fri, 23 Jul 2004 23:35:43 EDT) NNTP-Posting-Date: Fri, 23 Jul 2004 23:35:43 EDT Organization: Road Runner High Speed Online http://www.rr.com Xref: g2news1.google.com comp.lang.ada:2370 Date: 2004-07-24T03:35:43+00:00 List-Id: In article , Keith Boruff wrote: > Hey all, > [...] > In my studies this evening, I wrote the following trivial program to > force a constraint error: > > with Ada.Text_Io; use Ada.Text_Io; > > procedure Constraint_Tests is > > type Exam_Mark is range 0 .. 100; > > English : Exam_Mark := 72; > Math : Exam_Mark := 68; > Computing : Exam_Mark := 76; > Average : Exam_Mark; > > begin > > Put("Average exam mark is "); > > -- I expect a constraint exception in this statement > -- because English + Math + Computing > 100 > -- NOTE: On my machine, Exam_Mark'Base'Last = 127 > Average := (English + Math + Computing) / 3; > Put_line(Exam_Mark'Image(Average)); > > end Constraint_Tests; > > Ok, the problem I'm having is that I'm expecting a constraint exception > to occur because (English + Math + Computing) = 216 and the largest > value of its base is 127 (on my machine). > > However, I'm not getting this at all. I get no constraint errors > whatsoever and what's worse, Average = 243. First of all, why didn't I > get a constraint error here (since Average is of type Exam_Mark)? > Secondly, where in the heck did 243 come from? > > Now, if I omit the statement in the code and instead write: > Average := English + Math + Computing; > > I get a constraint error as expected. When I use the '/' operator > though, all bets are off. > > Is there something I'm not seeing here? Is there something about the / > operator I should know about? By default, GNAT disables overflow checking; you can enable it with the compiler switch -gnato. See section 3.2.5, "Run-Time Checks," in the "GNAT User's Guide" for more on the distiction between overflow and other constraint checks. [...] > Thanks, > Keith Boruff John ---- jmatthews at wright dot edu www dot wright dot edu/~john.matthews/