comp.lang.ada
 help / color / mirror / Atom feed
From: Marius Amado Alves <amado.alves@netcabo.pt>
To: comp.lang.ada@ada-france.org
Subject: Re: The right way to handle this difference in Ada
Date: Fri, 23 Jul 2004 13:47:41 +0100
Date: 2004-07-23T13:47:41+01:00	[thread overview]
Message-ID: <mailman.38.1090586871.416.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <4O%Lc.147269$JR4.139335@attbi_s54>

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.





  reply	other threads:[~2004-07-23 12:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-22 10:23 The right way to handle this difference in Ada vic
2004-07-22 11:53 ` Eric Jacoboni
2004-07-22 12:07 ` Nick Roberts
2004-07-22 13:03 ` Dmitry A. Kazakov
2004-07-22 13:22   ` Eric Jacoboni
2004-07-22 13:34     ` Dmitry A. Kazakov
2004-07-22 13:34 ` Björn Persson
2004-07-22 17:52 ` Georg Bauhaus
2004-07-22 18:10   ` Georg Bauhaus
2004-07-23  3:41 ` Steve
2004-07-23 12:47   ` Marius Amado Alves [this message]
2004-07-24  3:14     ` Robert I. Eachus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox