comp.lang.ada
 help / color / mirror / Atom feed
* Unsigned Integer Restraint Errors
@ 2007-03-12 15:07 frikk
  2007-03-12 16:27 ` Georg Bauhaus
                   ` (3 more replies)
  0 siblings, 4 replies; 30+ messages in thread
From: frikk @ 2007-03-12 15:07 UTC (permalink / raw)


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




^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2007-03-16 13:38 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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