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,b2923d60cb81694b X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!s48g2000cws.googlegroups.com!not-for-mail From: "Adam Beneschan" Newsgroups: comp.lang.ada Subject: Re: Unsigned Integer Restraint Errors Date: 12 Mar 2007 10:17:54 -0700 Organization: http://groups.google.com Message-ID: <1173719873.998022.142450@s48g2000cws.googlegroups.com> References: <1173712032.183064.264340@8g2000cwh.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1173719896 10468 127.0.0.1 (12 Mar 2007 17:18:16 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 12 Mar 2007 17:18:16 +0000 (UTC) In-Reply-To: <1173712032.183064.264340@8g2000cwh.googlegroups.com> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: s48g2000cws.googlegroups.com; posting-host=66.126.103.122; posting-account=cw1zeQwAAABOY2vF_g6V_9cdsyY_wV9w Xref: g2news1.google.com comp.lang.ada:14484 Date: 2007-03-12T10:17:54-07:00 List-Id: On Mar 12, 8:07 am, "frikk" 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;