comp.lang.ada
 help / color / mirror / Atom feed
From: DarthBob88 <darthbob88@gmail.com>
Subject: Re: "Constraint Error: Range Check" That Does Not Always Occur
Date: Thu, 22 Nov 2007 19:48:21 -0800 (PST)
Date: 2007-11-22T19:48:21-08:00	[thread overview]
Message-ID: <211e539c-81a8-4598-9acd-33907d03ad96@d4g2000prg.googlegroups.com> (raw)
In-Reply-To: uwssa4bbn.fsf@earthlink.net

On Nov 22, 6:14 am, Matthew Heaney <matthewjhea...@earthlink.net>
wrote:
> DarthBob88 <darthbo...@gmail.com> writes:
> > procedure test is
> >    subtype Die is Integer range 1..6;
> >    --Declare that anything of type Die may be
> >    --an integer, value between 1 and 6, inclusive, and sod-all else.
>
> That's fine.
>
> >    subtype Dice is Integer range 3*Die'First .. 3*Die'Last;
> >    --Declares that Dice may be anything from 3 to 18.
>
> But you sum 4 dice; that's a min of 4 * 1 and a max of 4 * 6 -- why did you
> make your Dice subtype too small?
That would because I cut'n'pasted it from a program to simulate
rolling two dice. As for the 3 and 4 discrepancy, that's just
stupidity on my part. I was trying to simulate the sum of three dice,
chosen from four that were rolled. Hence, that little idiocy.
*facepalm*

> >    package Random_Die is new Ada.Numerics.Discrete_Random (Die);
> >    use Random_Die;
> >    --Random generator; produces an integer between 1 and 6
>
> Right.
>
> >    sum : Integer := 0;
> >    G: Generator;
> >            D1 : Dice;
> >    D2 : Dice;
> >    D3 : Dice;
> >    D4 : Dice;
> >    --Declare four Dice
>
> --or:   D1, D2, D3, D4 : Die;  -- DICE IS THE WRONG SUBTYPE
> --      Sum : Dice;  -- subtype should be 4 .. 24
>
> > begin
> >    Reset(G);
> >    D1 := Random(G);
> >    D2 := Random(G);
> >    D3 := Random(G);
> >    D4 := Random(G);
> >    --I Haven't found the error just yet, but a Constraint_Error
> >    --Occasionally comes up for the previous 4 lines
> >    --I haven't been able to find it online, but I presume
> >    --It's because the generator comes up with a number outside of [1,6]
>
> The generator will return you a random number between 1 and 6 (inclusive).
>
> Your Die subtype has a minimum bounds of 1, so the random number can (and will)
> return values that include the values 1 and 2.
>
> Your Dice subtype (to which you assign the value returned by the generator) has
> a minimum bound of 3.  So when the random number generator returns a value less
> then 3, you get C_E during the assignment.
>
> Just change the declarations of Dx to use subtype Die instead of Dice, and then
> all is well.
Thank you kindly, sir. Ladies and gentlemen, this is why you should
not code at 0-dark-30, because you make idiotic mistakes like this,
instead of thinking through them.
> >    sum := D1 + D2 + D3 + D4;
> >    --Debugging
> >    Ada.Integer_Text_IO.Put(D1);
> >    Ada.Integer_Text_IO.Put(D2);
> >    Ada.Integer_Text_IO.Put(D3);
> >    Ada.Integer_Text_IO.Put(D4);
> >    Ada.Integer_Text_IO.Put(sum);
> > end test;
> > --compile Ada programs with gnat make <foo>.adb
>
> > If it helps any, I'm using the Windows version of GNAT on XP. Thanks
> > in advance.



      reply	other threads:[~2007-11-23  3:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-22  6:42 "Constraint Error: Range Check" That Does Not Always Occur DarthBob88
2007-11-22  7:38 ` Damien Carbonne
2007-11-22  9:12   ` anon
2007-11-22  9:56   ` Stephen Leake
2007-11-26 18:38   ` Adam Beneschan
2007-11-22  9:47 ` Stuart
2007-11-22 14:14 ` Matthew Heaney
2007-11-23  3:48   ` DarthBob88 [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox