From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f584bf624aabe591 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-05-16 06:56:04 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cpk-news-hub1.bbnplanet.com!cambridge1-snf1.gtei.net!news.gtei.net!bos-service1.ext.raytheon.com!dfw-service2.ext.raytheon.com.POSTED!not-for-mail Message-ID: <3CE3BA6D.8E4925FB@raytheon.com> From: Mark Johnson X-Mailer: Mozilla 4.76 [en] (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Signed integer to modular type conversion References: <3CE26A21.3EC6F802@raytheon.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Thu, 16 May 2002 08:55:57 -0500 NNTP-Posting-Host: 192.27.48.39 X-Complaints-To: news@ext.ray.com X-Trace: dfw-service2.ext.raytheon.com 1021557364 192.27.48.39 (Thu, 16 May 2002 08:56:04 CDT) NNTP-Posting-Date: Thu, 16 May 2002 08:56:04 CDT Organization: Raytheon Company Xref: archiver1.google.com comp.lang.ada:24196 Date: 2002-05-16T08:55:57-05:00 List-Id: 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