From: "Robert I. Eachus" <rieachus@comcast.net>
To: kboruff@optonline.net
Subject: Re: Should be getting a constraint error in this code
Date: Fri, 23 Jul 2004 23:33:14 -0400
Date: 2004-07-23T23:33:14-04:00 [thread overview]
Message-ID: <4101D87A.8040609@comcast.net> (raw)
In-Reply-To: <gTjMc.2800$q9.4134490@news4.srv.hcvlny.cv.net>
Keith Boruff wrote:
> Hey all,
>
> Forgive me for posting such a trivial problem here but I couldn't seem
> to find an answer in any of the FAQs I know of nor DejaNews.
>
> Though I'm not a novice programmer, I am a novice Ada programmer.
I suspect that the problem is that you are using GNAT, and forgot to
read the fine manual. If you expect integer overflow to be detected,
you need to run gnatmake -gnato
The historical reasons for this are now ancient history. The
alternative is to use pragma Unsuppress. If you insert it here:
>
> 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
pragma Unsuppress(Overflow_Check);
>
> 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).
Then you should get the result you expect. I get:
F:\Ada\Test>constraint_tests
constraint_tests
Average exam mark is
raised CONSTRAINT_ERROR : constraint_tests.adb:21 overflow check failed
with either pragma Unsuppres, or -gnato, and 243 without.
--
Robert I. Eachus
"The flames kindled on the Fourth of July, 1776, have spread over too
much of the globe to be extinguished by the feeble engines of despotism;
on the contrary, they will consume these engines and all who work them."
-- Thomas Jefferson, 1821
next prev parent reply other threads:[~2004-07-24 3:33 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-07-24 2:32 Should be getting a constraint error in this code Keith Boruff
2004-07-24 3:33 ` Robert I. Eachus [this message]
2004-07-24 3:35 ` John B. Matthews
2004-07-24 15:08 ` Björn Persson
2004-07-25 13:54 ` Martin Krischik
2004-07-25 15:14 ` Nick Roberts
-- strict thread matches above, loose matches on Subject: below --
2004-07-26 6:42 Christoph Karl Walter 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