comp.lang.ada
 help / color / mirror / Atom feed
From: adam@irvine.com (Adam Beneschan)
Subject: Signed integer to modular type conversion
Date: 14 May 2002 19:53:04 -0700
Date: 2002-05-15T02:53:04+00:00	[thread overview]
Message-ID: <b4682ab7.0205141853.7e835874@posting.google.com> (raw)

Supposing Standard.Integer is a 32-bit signed integer type.  I have a
modular type

    type ModType is mod 2**32;

    X : Integer;
    Y : ModType;

I'd like to set Y := X mod 2**32 (which should essentially just treat
X as an unsigned integer without changing the data).  However, I can't
figure out a good way to do this in Ada.  Using "mod" won't work,
because there is no integer type that can be used as a parameter to
"mod" that will take 2**32 as a parameter (I'm assuming that 32 bits
is the largest integer type supported by the compiler).  A type
conversion won't work, because it will raise Constraint_Error if X is
negative.  I could do something like

    if X < 0 then
	Y := ModType (X + ModType'Modulus/2) + ModType'Modulus/2;
    else
	Y := ModType (X);
    end if;

but this seems awfully silly for something that really should be just
a no-op on the machine.

Am I missing something, or is Unchecked_Conversion (or Pragma
Suppress) really the only reasonable way to accomplish this?  Unless
I've forgotten something, it seems quite odd that Ada provides modular
types but does not provide a solution for this (other than going
through the "back door" by using Unchecked_Conversion or Suppress).

If there is no solution, I'd like to propose adding an attribute so
that you could write something like

    Y := ModType'Mod (X);

This attribute would be defined for modular types and would return
X/ModType'Modulus.  I know, "Mod" is a bad name for the attribute.

My apologies if this has been discussed at length before.

				-- Adam



             reply	other threads:[~2002-05-15  2:53 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-15  2:53 Adam Beneschan [this message]
2002-05-15  3:34 ` Signed integer to modular type conversion Jim Rogers
2002-05-15 14:01 ` Mark Johnson
2002-05-16  0:02   ` Adam Beneschan
2002-05-16  2:26     ` Steven Deller
2002-05-16  9:53     ` Robert Dewar
2002-05-16 13:46       ` Steve Doiel
2002-05-16 14:00       ` Mark Johnson
2002-05-16 15:52       ` Adam Beneschan
2002-05-17  2:26         ` Robert Dewar
2002-05-17 16:44           ` Frank J. Lhota
2002-05-16 13:55     ` Mark Johnson
2002-05-15 19:12 ` Robert Dewar
2002-05-15 20:28 ` Adam Beneschan
  -- strict thread matches above, loose matches on Subject: below --
2002-05-15  5:43 Grein, Christoph
2002-05-16  4:39 Grein, Christoph
2002-05-16 14:13 ` Mark Johnson
replies disabled

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