comp.lang.ada
 help / color / mirror / Atom feed
From: "Steven Deller" <deller@smsail.com>
Subject: RE: Signed integer to modular type conversion
Date: Wed, 15 May 2002 22:26:25 -0400
Date: 2002-05-15T22:26:25-04:00	[thread overview]
Message-ID: <mailman.1021519682.22411.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: <b4682ab7.0205151602.3c564c9e@posting.google.com>

Compute in "integer" (the wider type) and then coerce into your modtype.

Y := modtype( X mod 2**32 ) ;
Z := modtype( integer(X) + Y ) ;

Makes it clear that the computation is done as "integer" with an
expected result that fits into "modtype".

You could also do:
   function "+" ( L : modtype ; R : integer ) return modtype is
   begin
       return modtype( integer(L) + R ) ;
   end "+"; 

and then write
    z := x + y ;

But unless mixing the two types is done a lot and you write "all" mixing
functions in a package (which can lead to disambiguation difficulties),
this is not good technique.

Some of your difficulty may be from looking at types as simply sets of
"the same" numeric values, rather than as abstractions of distinctly
different values that just happen to use the same "untyped" number set
to name the values.  

When you leave out abstraction, modtype simply looks like an integer
with some values taken out, and "+" looks like a single operator.

The real meaning of the values and operators depends on the abstraction
you are trying to model with each different type.  Usually, when the
full abstraction is considered, I find the problem of "mixing" types
resolves rather easily, or reveals an incorrect mix or incorrect
abstraction.

Regards,
Steve

> -----Original Message-----
> > Adam Beneschan wrote:
> > > 
> > > 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).
...
> OK, let me try a variation, and see if you still think 
> Unchecked_Conversion an appropriate way (or the best way) to 
> handle it.
> 
> I have two integer values.  One is in the range 0 .. 2**32-1, 
> and the other is in the range -2**31 .. 2**31-1.  I want to 
> find the mathematical sum of these two integers.  Let's 
> assume that I expect the result to be in the range 
> 0..2**32-1, and am not worried about what happens if it isn't.
> 
>     type ModType is mod 2**32;
>     X : ModType;
>     Y : Integer;
>     Z : ModType;
> 
> I want to compute Z := X + Y, but of course I can't write 
> this.  Z := X + ModType(Y) won't work if Y is negative; this 
> is what led me to ask the previous question, since I was 
> trying to find a way to convert Y to a ModType.
> 
> Again, I believe this is something that can be computed with 
> one machine instruction, so I'd like to avoid time-wasters like
> 
>     if Y < 0 then
> 	Z := X - ModType(-Y);
>     else
> 	Z := X + ModType(Y);
>     end if;
> 
> Would you still use something like Z := X + ToModType(Y) 
> [where ToModType is an instance of Unchecked_Conversion]?
> 
> 				-- Adam
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org 
> http://ada.eu.org/mailman/listinfo/comp.lang.ad> a
> 




  reply	other threads:[~2002-05-16  2:26 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-15  2:53 Signed integer to modular type conversion 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 [this message]
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