From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: The right way to handle this difference in Ada
Date: Fri, 23 Jul 2004 03:41:53 GMT
Date: 2004-07-23T03:41:53+00:00 [thread overview]
Message-ID: <4O%Lc.147269$JR4.139335@attbi_s54> (raw)
In-Reply-To: cdo4ic$q8f$1@e3k.asi.ansaldo.it
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" <ppp@ppp.it> 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
>
>
next prev parent reply other threads:[~2004-07-23 3:41 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 [this message]
2004-07-23 12:47 ` Marius Amado Alves
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