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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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!news1.google.com!news.maxwell.syr.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!news-fra1.dfn.de!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Unsigned Integer Restraint Errors Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1173712032.183064.264340@8g2000cwh.googlegroups.com> Date: Mon, 12 Mar 2007 19:00:05 +0100 Message-ID: NNTP-Posting-Date: 12 Mar 2007 19:00:03 CET NNTP-Posting-Host: a302b5da.newsspool1.arcor-online.net X-Trace: DXC=Qd2__;a1\BH3YbN=gZ0LSBW:aDNcfSJ;bb[eIRnRBaCd On 12 Mar 2007 08:07:12 -0700, frikk wrote: > 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. OK, this requires some clarification 1. Modular types cannot overflow per mathematical definition of. They form a ring closed for +,-,*,/ (except zero divide). 2. When you input a modular type you cannot get an out of range number for the reason 1. You can have it only when you convert a number from some *different* type back to the modular type or else when you constrain the modular type to a subtype of lesser range. 3. Unary "-" is an operation in Ada. See below. > 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; Here you don't get overflow because it has the semantics: -(5). The unary minus here has the meaning of the ring mod 2**64 which is different from unary minus of integer, see RM 4.5.4. The semantics you probably have implied is: test : UNSIGNED_LONG_INT := UNSIGNED_LONG_INT (Integer'(-5)); i.e. make *integer* -5 and convert it to test. This would indeed cause an error, as expected. > 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; This shall not raise Constraint_Error. The result should be 2**64 - 5, see RM 4.5.4. It seems that Windows GCC 3.4.6 for GNAT GPL 2006 (20060522) has a bug here for 2**64 modulus. With lesser modulus it works correct. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de