From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Types with physical dimension
Date: 4 Oct 94 11:51:07
Date: 1994-10-04T11:51:07+00:00 [thread overview]
Message-ID: <EACHUS.94Oct4115107@spectre.mitre.org> (raw)
In-Reply-To: sal714@rs710.gsfc.nasa.gov's message of 03 Oct 1994 17:31:09 GMT
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.
Paul Hilfinger did a nice paper on using discriminants to check
dimensionality. I don't have the paper in front of me but it works
something like this:
type Measure(Length,Mass,Time: Integer) is record
Value: Float;
end record;
subtype Meters is Measure(1,0,0);
subtype Kilograms is Measure(0,1,0);
subtype Time is Measure(0,0,1);
...etc.
Now you can define all the standard arithmetic operations on type
Measure so that they keep the discriminants correct:
function "*"(L,R: Measure) return Measure is
begin
return(L.Length+R.Length,L.Mass+R.Mass,L.Time+R.Time,L.Value*R.Value);
end "*";
function "+"(L,R: Measure) return Measure is
begin
if L.Length /= R.Length or L.Mass /= R.Mass or L.Time /= R.Time
then raise Constraint_Error; end if;
return L.Value+R.Value;
end "+";
In theory, if you use pragma INLINE, a good compiler should never
store the discriminants and eliminate all the discriminant related
code at compile time. In practice, when I used this technique, I had
two versions of the specification of the types package, one with the
discriminants and one which said "type Measure is Float;" and kept all
the subtype declarations. I used the full package during unit test,
and the streamlined package thereafter.
The only problem I had was with square roots. I thought about
changing the package to double all the dimensional values, so length
would be (2,0,0), but I never did.
--
Robert I. Eachus
with Standard_Disclaimer;
use Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...
next prev parent reply other threads:[~1994-10-04 11:51 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 [this message]
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
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
1994-09-28 19:28 Paul Pukite
[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
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox