comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <sb463ba@l1-hrz.uni-duisburg.de>
Subject: Re: The right way to handle this difference in Ada
Date: Thu, 22 Jul 2004 17:52:33 +0000 (UTC)
Date: 2004-07-22T17:52:33+00:00	[thread overview]
Message-ID: <cdout1$lkn$1@a1-hrz.uni-duisburg.de> (raw)
In-Reply-To: cdo4ic$q8f$1@e3k.asi.ansaldo.it

vic <ppp@ppp.it> wrote:
: 
: 
: Please consider this type:
: 
: type unsigned_int_12_type is range 0..(2*12)-1;

A step forward towards an Ada solution is stating
what you need this type for. So for what are objects of
this type used?

It is likely easier then for people to hint at
a language representation that suits your needs.
If you just "port" the C++ solution to what you think
is the "same" Ada solution, you might miss valuable
facilities of Ada. Coding might also become much more
difficult than it needs to be.

: c := a-b
: 
: 1) which should be the type of c? I think it should be something like:

What it should be depends what c is supposed to be.
For example, if you calculate in units of apples, then
  "a apples - b apples"
cannot easily be a sensible expression if a > b,
numerically. So what is c supposed to be?

If you include debt (in apples) then c might be the
number of apples that someone owes someone else.
So there exists a common "counting type" for the
number a, b, and c. In this case I'd choose a reasonable
range for possible numbers of apples, say

  type Apple_Count is range -10_000 .. 20_000;

and then, for measuring existing apples,

  subtype Existing_Apples is Apple_Count range 0 .. Apple_Count'last;

(There are no negative number included since obviously
there are no apples that don't exist.) Then,

  c: Apple_Count;
  a, b: Existing_Apples;

  ...

  c := a - b;

Or do you need 12 bits for some hardware representation?
In this case you can use a modular type as has been demonstrated
in another response, a packed array of Boolean values,
or a record with a representation clause (similar to a
bitfield in C.)

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

If you want "-" to convert to the type of c, I think it might help
the reader of the source code if instead you convert the
operands so that "-" becomes a computation in the type of c.


-- Georg



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