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.3 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, REPLYTO_WITHOUT_TO_CC autolearn=no 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-15 20:28:03 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!195.25.12.36!oleane.net!oleane!freenix!enst!enst.fr!not-for-mail From: "Steven Deller" Newsgroups: comp.lang.ada Subject: RE: Signed integer to modular type conversion Date: Wed, 15 May 2002 22:26:25 -0400 Organization: ENST, France Sender: comp.lang.ada-admin@ada.eu.org Message-ID: Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Host: marvin.enst.fr Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: avanie.enst.fr 1021519682 94292 137.194.161.2 (16 May 2002 03:28:02 GMT) X-Complaints-To: usenet@enst.fr NNTP-Posting-Date: Thu, 16 May 2002 03:28:02 +0000 (UTC) Return-Path: X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2627 Importance: Normal In-Reply-To: X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org X-Mailman-Version: 2.0.8 Precedence: bulk X-Reply-To: List-Help: List-Post: List-Subscribe: , List-Id: comp.lang.ada mail<->news gateway List-Unsubscribe: , Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org Xref: archiver1.google.com comp.lang.ada:24153 Date: 2002-05-15T22:26:25-04:00 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 >