comp.lang.ada
 help / color / mirror / Atom feed
From: "Adam Beneschan" <adam@irvine.com>
Subject: Re: Unsigned Integer Restraint Errors
Date: 12 Mar 2007 10:17:54 -0700
Date: 2007-03-12T10:17:54-07:00	[thread overview]
Message-ID: <1173719873.998022.142450@s48g2000cws.googlegroups.com> (raw)
In-Reply-To: <1173712032.183064.264340@8g2000cwh.googlegroups.com>

On Mar 12, 8:07 am, "frikk" <frik...@gmail.com> wrote:
> Hello Everyone!
>
> I'm having a trivial difficulty with Ada. I am working with a 64 bit
> unsigned integer, and of course I would like to know if the input to
> this unsigned integer is out of range. I am having two issues.  The
> first is that I cannot get ada to raise a constraint error unless I
> make a subtype and state the range of being a finite number (but only
> in some circumstances, I'll explain in a second).  The second thing is
> that when I do get it to raise a constraint error with the finite
> range, the exception isn't handled correctly. I think it may have
> something to do with me using 64 bit unsigned integers.
>
> Here is my unmodified code which I would expect to work. Note that I
> set the value of 'test' twice to what I wouuld expect to be an invalid
> value:
>
> with Ada.Text_IO;
> use Ada.Text_IO;
>
> procedure Prime_Bits is
>    -- Declare a 64 bit unsigned integer
>    type UNSIGNED_LONG_INT is mod 2**64;
>    test : UNSIGNED_LONG_INT := -5;

I think your error comes from seeing "-5" and thinking this is the
number -5.  It's not.  -5 in Ada is not a numeric literal that
represents the number "negative five"; rather, it's the result of
applying the unary function "-" to the number 5.  And the way this is
written, the function that gets applied is the one that operates on
the modular type:

   function "-" (Left : UNSIGNED_LONG_INT) return UNSIGNED_LONG_INT;

and, as Georg has already pointed out, the predefined functions for
modular types always compute the result modulo the modulus.  See the
note in RM 4.5.4(3), which makes this more explicit.

This would give you a Constraint_Error, if it compiled:

    test: UNSIGNED_LONG_INT := UNSIGNED_LONG_INT (Integer' (-5));

because now the "-" that gets applied is the "-" that's defined for
the type Integer, and the result of the "-" operator is the Integer
-5, and the type conversion *does* raise Constraint_Error because -5
isn't in range.  In reality, this won't even compile, because the
compiler can determine that this evaluating this static expression
will raise Constraint_Error, and this makes the program illegal by RM
4.9(34).

This should raise Constraint_Error:

    X : integer := 5;
    Test : UNSIGNED_LONG_INT := UNSIGNED_LONG_INT (-X);

and this should not:

    X : integer := 5;
    Test : UNSIGNED_LONG_INT := - UNSIGNED_LONG_INT (X);

One more point: In the example that does raise Constraint_Error, your
exception handler that outputs the string "Constraint_Error!!!!" would
*not* be executed.  When an exception occurs in a declaraton, the
exception handler that gets executed, if any, must be in a scope that
*encloses* the scope with the declaration.  So to get your own
exception handler to work on an exception raised by a declaration,
you'd need to do something like this:

    procedure Prime_Bits is
    begin
       declare
           X : Integer := 5;
           Test : UNSIGNED_LONG_INT := UNSIGNED_LONG_INT (-X);
       begin
           null;
       end;
    exception
        when Constraint_Error => Put_Line("Constraint Error!!!");
    end;

Hope this helps clear things up,

                                 -- Adam

P.S. If you really want a good demonstration of what I mean about the
unary "-" operator, try this:

with Ada.Text_IO;
procedure Test1 is
    package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
    type New_Int is new Integer;
    function "-" (Left : New_Int) return New_Int is
    begin
       return Left * 2;
    end "-";
    A : New_Int := -6;
    B : Integer := Integer (A);
begin
    Int_IO.Put (B);
end Test1;






  parent reply	other threads:[~2007-03-12 17:17 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-12 15:07 Unsigned Integer Restraint Errors frikk
2007-03-12 16:27 ` Georg Bauhaus
2007-03-12 17:17 ` Adam Beneschan [this message]
2007-03-12 17:23 ` Adam Beneschan
2007-03-12 18:11   ` frikk
2007-03-12 20:00     ` frikk
2007-03-12 20:07       ` Adam Beneschan
2007-03-12 18:00 ` Dmitry A. Kazakov
2007-03-12 19:00   ` Martin Krischik
2007-03-12 21:13     ` Dmitry A. Kazakov
2007-03-12 19:13   ` frikk
2007-03-12 19:22     ` Randy Brukardt
2007-03-13  3:13       ` Jeffrey R. Carter
2007-03-13  3:00         ` Randy Brukardt
2007-03-13 12:09           ` frikk
2007-03-13 14:58             ` frikk
2007-03-13 15:31               ` frikk
2007-03-13 15:59                 ` Robert A Duff
2007-03-13 16:18                 ` Dmitry A. Kazakov
2007-03-13 16:21                 ` Jeffrey R. Carter
2007-03-13 16:04               ` Adam Beneschan
2007-03-13 16:41                 ` Adam Beneschan
2007-03-13 16:42                   ` Adam Beneschan
2007-03-14 14:06                     ` frikk
2007-03-13 17:23                 ` Dmitry A. Kazakov
2007-03-13 17:31                   ` Adam Beneschan
2007-03-14  0:54                   ` Jeffrey R. Carter
2007-03-16 13:38                   ` frikk
2007-03-13 16:16           ` Jeffrey R. Carter
2007-03-12 21:04     ` Dmitry A. Kazakov
replies disabled

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