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,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fcf8ea94b94d6941 X-Google-Attributes: gid103376,public From: Ole-Hjalmar Kristensen Subject: Re: Modular type. What is it and why? Date: 1999/04/07 Message-ID: #1/1 X-Deja-AN: 463632480 X-NNTP-Posting-Host: fwall.clustra.com References: <7ean3c$79m$1@eol.dd.chalmers.se> <7eb2iq$jc3@hobbes.crc.com> <7ede2p$pd1$1@its.hooked.net> <370A4B3A.5EDB3B37@pwfl.com> <7edk6p$1j7$1@its.hooked.net> Newsgroups: comp.lang.ada X-Complaints-To: abuse@telia.no Date: 1999-04-07T00:00:00+00:00 List-Id: "Mike Silva" writes: > Marin David Condic wrote in message <370A4B3A.5EDB3B37@pwfl.com>... > >Mike Silva wrote: > >> Now with Ada I'll be able to write (assuming 'index' is the appropriate > >> modular type): > >> > >> case ( key ) is > >> when ARROW_DOWN => > >> index := index + 1; > >> when ARROW_UP => > >> index := index - 1; > >> end case; > >> > >> Now -that's- what I call progress! > >> > > > >My C is pretty rusty - not having done anything serious in it in at > >least 10 years - but wasn't there some kind of type/operation that > >allowed for wraparound semantics?... > > Well, if you're going up you can do: > > index = ( index + 1 ) % NUM_ELEMENTS; /* where % is the MOD operator */ > > but you can't do: > > index = ( index - 1 ) % NUM_ELEMENTS; /* won't handle 0 -> > NUM_ELEMENTS - 1 correctly */ > Actually, if index is unsigned, then it *will* handle 0 -> NUM_ELEMENTS - 1 correctly. > As Ole-Hjalmar Kristensen commented, the nice Ada solution doesn't work if > NUM_ELEMENTS varies at runtime (at least, it appears that a modular type > can't be defined dynamically -- correct?), but that's not the usual case I > run across. Agreed, I just thought I would mention it. > > Mike > > > -- E pluribus Unix