Should be getting a constraint error in this code
From: Keith Boruff (kboruff_at_optonline.net)
Date: 07/24/04
- Next message: Robert I. Eachus: "Re: reading a text file into a string"
- Previous message: Randy Brukardt: "Re: reading a text file into a string"
- Next in thread: Robert I. Eachus: "Re: Should be getting a constraint error in this code"
- Reply: Robert I. Eachus: "Re: Should be getting a constraint error in this code"
- Reply: John B. Matthews: "Re: Should be getting a constraint error in this code"
- Reply: Björn Persson: "Re: Should be getting a constraint error in this code"
- Maybe reply: Christoph Karl Walter Grein: "Re: Should be getting a constraint error in this code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 24 Jul 2004 02:32:44 GMT
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.
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?
Or... could this possibly be a bug in the compiler itself (using GNAT)?
I'm embarrassed to be posting such a trivial program but it really
bothers me that the behavior is not what I expect.
If anyone has any suggestions, I'd appreciate some info on the matter...
even if it's to tell me to RTFM (... as long as you provide a link).
Thanks,
Keith Boruff
- Next message: Robert I. Eachus: "Re: reading a text file into a string"
- Previous message: Randy Brukardt: "Re: reading a text file into a string"
- Next in thread: Robert I. Eachus: "Re: Should be getting a constraint error in this code"
- Reply: Robert I. Eachus: "Re: Should be getting a constraint error in this code"
- Reply: John B. Matthews: "Re: Should be getting a constraint error in this code"
- Reply: Björn Persson: "Re: Should be getting a constraint error in this code"
- Maybe reply: Christoph Karl Walter Grein: "Re: Should be getting a constraint error in this code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]