comp.lang.ada
 help / color / mirror / Atom feed
From: Vinzent 'Gadget' Hoefler <ada.rocks@jlfencey.com>
Subject: Re: Modular integers
Date: Tue, 28 Oct 2003 15:13:25 +0100
Date: 2003-10-28T15:13:25+01:00	[thread overview]
Message-ID: <bnltkl$11i07f$1@ID-175126.news.uni-berlin.de> (raw)
In-Reply-To: bnloh2$q3d$03$1@news.t-online.com

Lars wrote:

>Why couldn't the compiler insert code that checks what the upper limit of
>the type is,

It does, if necessary. AFAICS in your case it isn't: The upper limit
of your subtype is the same as that of the base type which is a
modular type.

So where would you expect the overflow? You don't get rid of the
modulo-operation just by defining a sub type.

>then substracts the current value of the variable from the
>upper limit to see if the difference is bigger or equal to the number you
>want to add? If it is not, it could throw an exception ...

Why should it? No thinkable result of the modulo 2**64 operation would
overflow your new range constraint for the subtype:

IOW: "(A + 1) mod 2**64 in 0 .. 2**64 - 1" is always true.

But try this with a subtype with a range only up to 2**64 - 2, this
*will* give you the expected overflow:

|with Ada.Text_IO;
|
|procedure t is
|   type    X_Base is mod 2**64;
|   subtype X_1    is X_Base range 0 .. 2**64 - 1;
|   subtype X_2    is X_Base range 0 .. 2**64 - 2;
|
|   A : X_Base;
|   B : X_1;
|   C : X_2;
|
|begin
|   A := X_Base'Last;
|   Ada.Text_IO.Put (X_Base'Image (A));
|   A := A + 1;
|   Ada.Text_IO.Put (X_Base'Image (A));
|   
|   B := X_1'Last;
|   Ada.Text_IO.Put (X_Base'Image (B));
|   B := B + 1;
|   Ada.Text_IO.Put (X_Base'Image (B));
|   
|   C := X_2'Last;
|   Ada.Text_IO.Put (X_Base'Image (C));
|   C := C + 1;
|   Ada.Text_IO.Put (X_Base'Image (C));
|end t;

compiled with GNAT3.15p gives:

| 18446744073709551615 0 18446744073709551615 0 18446744073709551614
|
|raised CONSTRAINT_ERROR : t.adb:25 range check failed

Isn't that the expected result?


Vinzent.



  reply	other threads:[~2003-10-28 14:13 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-28 12:13 Modular integers Lars
2003-10-28 12:28 ` David C. Hoos
2003-10-28 12:48   ` Lars
2003-10-28 14:13     ` Vinzent 'Gadget' Hoefler [this message]
2003-10-28 16:55       ` Lars
2003-10-28 18:50         ` David C. Hoos
2003-10-28 20:46           ` Lars
2003-10-29  8:30         ` Vinzent 'Gadget' Hoefler
2003-10-29 10:39           ` Jean-Pierre Rosen
2003-10-29 12:16             ` Vinzent 'Gadget' Hoefler
2003-10-29  1:51 ` Raising exceptions (was: Modular integers) Jeffrey Carter
2003-10-29  3:02   ` Raising exceptions Hyman Rosen
replies disabled

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