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=-2.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Thread: 103376,34a625e98d6a8792 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!news.agarik.com!usenet-fr.net!enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: Marius Amado Alves Newsgroups: comp.lang.ada Subject: Re: The right way to handle this difference in Ada Date: Fri, 23 Jul 2004 13:47:41 +0100 Organization: Cuivre, Argent, Or Message-ID: References: <4O%Lc.147269$JR4.139335@attbi_s54> NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: melchior.cuivre.fr.eu.org 1090586872 51138 212.85.156.195 (23 Jul 2004 12:47:52 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Fri, 23 Jul 2004 12:47:52 +0000 (UTC) To: comp.lang.ada@ada-france.org Return-Path: User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en-us, en In-Reply-To: <4O%Lc.147269$JR4.139335@attbi_s54> X-Virus-Scanned: by amavisd-new-20030616-p7 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Gateway to the comp.lang.ada Usenet newsgroup" List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: g2news1.google.com comp.lang.ada:2357 Date: 2004-07-23T13:47:41+01:00 Steve wrote: > When programming in C/C++ you think in terms of the small fixed set of data > types that map to the hardware... > > C/C++ thinking: > Let's see... I have a value that is going to be in the range of 0 to 457. > That means I'll need to use a 16 bit value. I'll use a short or an unsigned > short, since I know that on the machine I am programming, short values are > 16 bit. > > Ada thinking: > Let's see... I have a value that is going to be in the range of 0 to 457. > I'll define a tyhpe and let the compiler worry about it. > > type My_Type is range 0 .. 457; > > The only time you really need to worry about the number of bits is when > you are programming interfaces. This is wise advice up until here. Original Poster, follow it. Welcome to the Ada Way. > Back to your question. Your question leads to another question: what > should happen if I subtract two variables of type unsigned_int_12_type? No. You should ask: why do I want to subtract? What is the resulting type? An intermediary value in an expression? A definitive value? Of what type? > If you want the values to "wrap" like unsigned values in C++, you should > define your type using a "mod" type instead. If the result of your > subraction is always going to be a signed value in the range of > unsigned_int_12_type, then go ahead and define c as an unsigned_int_12_type. > If the difference is out of range, you'll get a runtime error. > > Since you indicated your background is C++, I'm guessing you want the type > to behave more like an unsigned value in C++ and ignore out of range > conditions. If that is the case, change your definition to: > > type unsigned_int_12_type is mod 2**12; > > And define c of the same type... For some reason in this part of the reply Steve has regressed to what he himself calls "C/C++ thinking". Original Poster, ignore.