comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: The right way to handle this difference in Ada
Date: Thu, 22 Jul 2004 15:03:46 +0200
Date: 2004-07-22T15:03:46+02:00	[thread overview]
Message-ID: <15yc7cceofj1e$.ioaad0q1si6i.dlg@40tude.net> (raw)
In-Reply-To: cdo4ic$q8f$1@e3k.asi.ansaldo.it

On Thu, 22 Jul 2004 12:23:09 +0200, vic wrote:

> 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?

It depends!

1. In C++ spirit I would expect something like

   type unsigned_int_12_type is mod 2**12;

and the types of a,b,c all same. So that:

   a : unsigned_int_12_type := 1;
   b : unsigned_int_12_type := 2;
   c : unsigned_int_12_type := a - b; -- yields 4095

2. Well, if c should be negative then probably:

   type int_12_type is range -2**12..2**12-1;
   subtype unsigned_int_12_subtype is
      int_12_type range 0..int_12_type'Last;

   a : unsigned_int_12_subtype := 1;
   b : unsigned_int_12_subtype := 2;
   c : int_12_type := a - b; -- yields -1

Here unsigned_int_12_subtype is a subtype. Though its values cannot be
negative, temporary values can be. It is similar to the solution of
proposed by Nick Roberts, but uses a separate type int_12_type instead of
standard Integer, mainly for strong typing sake.

> 2) And I should rename a "-" operation which gets two signed_int_12_type  as
> operands and returns signed_int_12_type as result?

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2004-07-22 13:03 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 [this message]
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
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