comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Unsigned Integer Restraint Errors
Date: Mon, 12 Mar 2007 19:00:05 +0100
Date: 2007-03-12T19:00:03+01:00	[thread overview]
Message-ID: <e9in1ny6b3gb.16e7shcfhyja8$.dlg@40tude.net> (raw)
In-Reply-To: 1173712032.183064.264340@8g2000cwh.googlegroups.com

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



  parent reply	other threads:[~2007-03-12 18:00 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
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 [this message]
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