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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,b2923d60cb81694b,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!8g2000cwh.googlegroups.com!not-for-mail From: "frikk" Newsgroups: comp.lang.ada Subject: Unsigned Integer Restraint Errors Date: 12 Mar 2007 08:07:12 -0700 Organization: http://groups.google.com Message-ID: <1173712032.183064.264340@8g2000cwh.googlegroups.com> NNTP-Posting-Host: 12.129.98.129 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1173712039 21633 127.0.0.1 (12 Mar 2007 15:07:19 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 12 Mar 2007 15:07:19 +0000 (UTC) User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727),gzip(gfe),gzip(gfe) X-HTTP-Via: 1.1 TRY0PX01 Complaints-To: groups-abuse@google.com Injection-Info: 8g2000cwh.googlegroups.com; posting-host=12.129.98.129; posting-account=192wHg0AAAAzciSzoZsEBI9bw5pVCopO Xref: g2news1.google.com comp.lang.ada:14478 Date: 2007-03-12T08:07:12-07:00 List-Id: 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; package LONG_IO is new Modular_IO(UNSIGNED_LONG_INT); use LONG_IO; begin Put("Minimum Value(uint64_t): "); Put(UNSIGNED_LONG_INT'FIRST); New_Line; Put("Maximum Value(uint64_t): "); Put(UNSIGNED_LONG_INT'LAST); New_Line; test := -5; Put(test); exception when Constraint_Error => Put("Constraint Error!!!!"); when others => Put("Unknown Error"); end Prime_Bits; ############## Output: M:\work\ada\prime_bits>prime_bits Minimum Value(uint64_t): 0 Maximum Value(uint64_t): 18446744073709551615 18446744073709551611 Here is the code for which I *can* generate a constraint error, however the error is not handled at all even though I have the handling in the right area (I think): 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; subtype SUB_UNSIGNED_LONG_INT is UNSIGNED_LONG_INT range 0 .. 5; test : SUB_UNSIGNED_LONG_INT := -5; package LONG_IO is new Modular_IO(SUB_UNSIGNED_LONG_INT); use LONG_IO; begin Put("Minimum Value(uint64_t): "); Put(SUB_UNSIGNED_LONG_INT'FIRST); New_Line; Put("Maximum Value(uint64_t): "); Put(SUB_UNSIGNED_LONG_INT'LAST); New_Line; test := -5; Put(test); exception when Constraint_Error => Put("Constraint Error!!!!"); when others => Put("Unknown Error"); end Prime_Bits; ################ Output: M:\work\ada\prime_bits>prime_bits raised CONSTRAINT_ERROR : prime_bits.adb:8 LASTLY: Please note that when I change the range value from 0 .. 5 to 0 .. 2**64-1, or 0 .. UNSIGNED_LONG_INT'Last, there is no constraint error raised. This is the same behavior as the first example. Thank you! Blaine