comp.lang.ada
 help / color / mirror / Atom feed
* Re: Signed integer to modular type conversion
@ 2002-05-16  4:39 Grein, Christoph
  2002-05-16 14:13 ` Mark Johnson
  0 siblings, 1 reply; 17+ messages in thread
From: Grein, Christoph @ 2002-05-16  4:39 UTC (permalink / raw)


From: Mark Johnson <mark_h_johnson@raytheon.com>
> PS: I don't generally recommend this solution because many use it to
> excess. Try...
>   with Unchecked_Conversion;
>   function "+" is new Unchecked_Conversion(Integer, Mod_Type);
> Then your example assignment will be...
>   Y := +X;
> The abuse comes from using this method for all sorts of type
> conversions, avoiding type safety, etc.

Did you really observe much abuse? This affords so much more wording than a 
simple type conversion, so why should someone abuse it?



^ permalink raw reply	[flat|nested] 17+ messages in thread
* Re: Signed integer to modular type conversion
@ 2002-05-15  5:43 Grein, Christoph
  0 siblings, 0 replies; 17+ messages in thread
From: Grein, Christoph @ 2002-05-15  5:43 UTC (permalink / raw)


> Am I missing something, or is Unchecked_Conversion (or Pragma
> Suppress)

No, you're not missing something. Unchecked_Conversion is just there to perform 
a "type cast" if the representation does not change.

pragma Suppress is there for different reasons.



^ permalink raw reply	[flat|nested] 17+ messages in thread
* Signed integer to modular type conversion
@ 2002-05-15  2:53 Adam Beneschan
  2002-05-15  3:34 ` Jim Rogers
                   ` (3 more replies)
  0 siblings, 4 replies; 17+ messages in thread
From: Adam Beneschan @ 2002-05-15  2:53 UTC (permalink / 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



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

end of thread, other threads:[~2002-05-17 16:44 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-16  4:39 Signed integer to modular type conversion Grein, Christoph
2002-05-16 14:13 ` Mark Johnson
  -- strict thread matches above, loose matches on Subject: below --
2002-05-15  5:43 Grein, Christoph
2002-05-15  2:53 Adam Beneschan
2002-05-15  3:34 ` 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

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