comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Engineering types hierarchy
Date: 1999/09/08
Date: 1999-09-08T00:00:00+00:00	[thread overview]
Message-ID: <37d663dd@news1.prserv.net> (raw)
In-Reply-To: 7r50i7$e893@svlss.lmms.lmco.com

In article <7r50i7$e893@svlss.lmms.lmco.com> , dkristol@see-my.sig (David 
Kristola) wrote:

> Hello,
>
> I am trying to put together a robust and easily usable
> hierarchy of types packages to support a collection of
> engineering units (kilograms, meters, seconds, etc.)
> in scalar, vector, and matrix forms.

> [snip]

> Is there a better way?


In general, I like to keep it simple, and just name (scalar) types with
their units:

  package Ownship is

     type Heading_In_Deg_Type_Base is
         delta 0.1 range -720.0 .. 720.0;

     for Heading_Type_In_Deg_Type_Base'Small use 0.1

     subtype Heading_In_Deg_Type is
       Heading_In_Deg_Type_Base
       range 0.0 .. Heading_In_Deg_Type_Base'Pred (360.0);


     function Get_Heading_In_Deg return Heading_In_Deg_Type;


     type Speed_In_Knots_Type is <whatever>;

     function Get_Speed_In_Knots return Speed_In_Knots_Type;

     ...

  end Ownship;

Naming selector functions with their units prevents this kind of error:

  declare
    Speed_In_MPH : constant Speed_In_MPH_Type :=
      Speed_In_MPH_Type (Ownship.Get_Speed);
  begin

This is a bug, because ownship speed is in knots.  Compare it to:

  declare
    Speed_In_MPH : constant Speed_In_MPH_Type :=
      Speed_In_MPH_Type (Ownship.Get_Speed_In_Knots .. huh???

and hopefully the programmer realizes immediately that there's a type
conversion issue.

When I need to go a units conversion, I just use a package of named numbers:

package Conversions.Speed is

   Knots_Per_KPH : constant := ...;

   KPH_Per_Knots : constant := 1.0 / Knots_Per_KPH;

   MPH_Per_KPH : constant := ...;

   KPH_Per_MPH : constant := 1.0 / MPH_Per_KPH;

   ...
end Conversions.Speed;


Actually, if you have a similar package for length conversions (and maybe
another for simple time conversions), you won't even really need a dedicated
package for speed conversions.

As you've already discovered, other approaches engender a combinatorial
explosion.  There's no way for you to know up front which conversions you're
going to need, so just declare a minimal set (for length, mass, and time,
say), and leave it up to the client to combine the primitive units
conversions into the complex units he needs locally.

It the time of conversion, I just declare the objects with their units and
units-and-type:

  declare
    Speed_In_Knots : constant Speed_In_Knots_Type :=
      Ownship.Get_Speed_In_Knots;

    Speed_In_Knots_As_MPH : constant Speed_In_MPH_Type'Base :=
      Speed_In_MPH_Type'Base (Speed_In_Knots);

    Speed_In_MPH : constant Speed_In_MPH_Type :=
       Speed_In_Knots_As_MPH * MPH_Per_Knots;
  begin
    Set_Speed (Of_Something, To => Speed_In_MPH);
  end;


I find that this simple approach works best, and is more efficient than
other, more complex schemes.  It may be more verbose, but the conversions
are done using baby steps, and I can tell exactly what's going on.

Matt




  parent reply	other threads:[~1999-09-08  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-08  0:00 Engineering types hierarchy David Kristola
1999-09-08  0:00 ` Robert Dewar
1999-09-09  0:00   ` David Kristola
1999-09-09  0:00     ` Robert Dewar
1999-09-10  0:00       ` David Kristola
1999-09-09  0:00     ` Robert Dewar
1999-09-08  0:00 ` Pat Rogers
1999-09-09  0:00   ` David Kristola
1999-09-08  0:00 ` Matthew Heaney [this message]
1999-09-09  0:00   ` David Kristola
1999-09-08  0:00 ` Marin David Condic
1999-09-09  0:00   ` David Kristola
1999-09-11  0:00     ` Richard D Riehle
1999-09-08  0:00 ` Hyman Rosen
1999-09-08  0:00   ` Matthew Heaney
1999-09-09  0:00 ` David Botton
1999-09-10  0:00   ` David Kristola
1999-09-10  0:00     ` Ted Dennison
  -- strict thread matches above, loose matches on Subject: below --
1999-09-09  0:00 Matthew Heaney
1999-09-09  0:00 ` Matthew Heaney
1999-09-09  0:00   ` Pat Rogers
1999-09-10  0:00   ` David Kristola
replies disabled

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