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,FREEMAIL_FROM autolearn=ham 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!wns13feed!worldnet.att.net!attbi_s54.POSTED!53ab2750!not-for-mail From: "Steve" Newsgroups: comp.lang.ada References: Subject: Re: The right way to handle this difference in Ada X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 Message-ID: <4O%Lc.147269$JR4.139335@attbi_s54> NNTP-Posting-Host: 24.21.42.251 X-Complaints-To: abuse@comcast.net X-Trace: attbi_s54 1090554112 24.21.42.251 (Fri, 23 Jul 2004 03:41:52 GMT) NNTP-Posting-Date: Fri, 23 Jul 2004 03:41:52 GMT Organization: Comcast Online Date: Fri, 23 Jul 2004 03:41:53 GMT Xref: g2news1.google.com comp.lang.ada:2355 Date: 2004-07-23T03:41:53+00:00 List-Id: 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. Back to your question. Your question leads to another question: what should happen if I subtract two variables of type unsigned_int_12_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. Steve (The Duck) "vic" wrote in message news:cdo4ic$q8f$1@e3k.asi.ansaldo.it... > Hello, > > I'm starting to develope in Ada 95, but as I come from C++ language I'm > having some difficulties about strong typing. > > > Please consider this type: > > type unsigned_int_12_type is range 0..(2*12)-1; > > and these 2 variables of the above type: > > a: unsigned_int_12_type; > b: unsigned_int_12_type; > > If the code must perform a difference between a and b, say: > > c := a-b > > 1) which should be the type of c? I think it should be something like: > > type signed_int_12_type is range -unsigned_int_12'last .. > unsigned_int_12'last. > > Is this right? > > 2) And I should rename a "-" operation which gets two signed_int_12_type as > operands and returns signed_int_12_type as result? > > > Thanks, > > vic > >