From: Mark Johnson <mark_h_johnson@raytheon.com>
Subject: Re: Signed integer to modular type conversion
Date: Thu, 16 May 2002 08:55:57 -0500
Date: 2002-05-16T08:55:57-05:00 [thread overview]
Message-ID: <3CE3BA6D.8E4925FB@raytheon.com> (raw)
In-Reply-To: b4682ab7.0205151602.3c564c9e@posting.google.com
Adam Beneschan wrote:
>
> 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.
Why not? You can if you define the "+" operator for the right parameter
as integer and the left as your ModType (and the associative version as
well...). I will also assume I have the unary plus operator
(Unchecked_Conversion) as previously described....
function "+"(Left: Integer; Right:ModType) return ModType is
Temp : ModType := +Left; -- convert to ModType
begin
return Temp+Right;
end "+";
Which converts the left operator to ModType and uses modular add to get
the result. Let's try some examples...
1+2000 is 2001
-1+2000 is 1999
The modular arithmetic should do the "right thing" for the unsigned
addition.
--Mark
next prev parent reply other threads:[~2002-05-16 13:55 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
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 [this message]
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