comp.lang.ada
 help / color / mirror / Atom feed
From: rosen@enst.fr (Jean-Pierre Rosen)
Subject: Re: Types with physical dimension
Date: 5 Oct 1994 09:38:05 +0100
Date: 1994-10-05T09:38:05+01:00	[thread overview]
Message-ID: <36tole$j5e@cyclope.enst.fr> (raw)
In-Reply-To: SAL714.94Oct3133110@rs710.gsfc.nasa.gov

In article <SAL714.94Oct3133110@rs710.gsfc.nasa.gov>, sal714@rs710.gsfc.nasa.gov (Stephen A. Leake) writes:
> I run robots with Ada, so I deal with real dimensions all the time. Very early
> on, I started to write a package that enforced dimensionality; I defined 
> METERS, METERS_PER_SECOND, etc. I quickly discovered that even simple equations
> need up to cubic dimensions, in all combinations of length, mass and time.
> Defining all these types and the associated operators was far too
> time-consuming to be worth it. So now I rely on code reviews and careful
> testing.
> 
> The moral; strong typing is nice, but is no substitute for careful design
> and thorough testing.
> 
There is another solution: run-time checking of dimensionality. You define a
type like:
   type Physical is
      record
         Length : Dimension;
         Time   : Dimension;
         Mass   : Dimension;
         Charge : Dimension;
         Value  : Float;
      end record;
 (Dimension being some Integer type).
You redefine operators so that "+" and "-" operate only if the dimensions are
the same (and raise Dimension_error otherwise), and "*" and "/" add 
(or subtract) dimensions.
Note that if you are concerned about efficiency, you use this type for 
debugging, and then change it to:
   type Physical is new Float;
for the working version (assuming a Value_of function to access the value).

Alternatively, you can make the dimensions discriminants; this will improve
safety and allow you to define nice subtypes like:
   subtype Meters is Physical(1, 0, 0, 0);
but will make moving to plane Floats more difficult.

Acknowledgements:
  I saw this idea in a paper long ago. As far as I recall, it was by
  N. Cohen. Norm, are you listening?





  parent reply	other threads:[~1994-10-05  8:38 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-09-27 22:18 Types with physical dimension Paul Graham
1994-09-28 13:59 ` Robert Dewar
1994-09-30  2:06   ` R_Tim_Coslet
1994-10-03 17:31 ` Stephen A. Leake
1994-10-04 11:51   ` Robert I. Eachus
1994-10-04 19:45     ` Mark A Biggar
     [not found]       ` <CxBBx8.7L@irvine.com>
1994-10-13 22:15         ` gamache
1994-10-12  3:43     ` Matt Kennel
1994-10-04 14:56   ` Robert Dewar
1994-10-05 14:53     ` Bob Gilbert
1994-10-05  8:38   ` Jean-Pierre Rosen [this message]
1994-10-05 10:35     ` Stephen J Bevan
1994-10-05 13:17       ` Jean-Pierre Rosen
1994-10-05 15:48     ` Norman H. Cohen
  -- strict thread matches above, loose matches on Subject: below --
1994-09-28 10:55 Simtel20 Transfer
1994-09-28 18:56 ` Mark A Biggar
1994-10-04  2:06 ` lmiller
     [not found] <GRAHAM.94Sep27181841@canopus.clsi.com>
1994-09-28 17:36 ` William Brennan
1994-09-28 21:41 ` Tucker Taft
1994-09-29  4:11   ` Robert Dewar
1994-09-29 11:19     ` Peter Hermann
1994-09-30 10:17       ` Dr John Stockton
1994-10-03  4:37       ` Robert Dewar
1994-09-29 13:37     ` Tucker Taft
1994-10-03  4:40       ` Robert Dewar
1994-09-28 19:28 Paul Pukite
replies disabled

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