From: mab@dst17.wdl.loral.com (Mark A Biggar)
Subject: Re: Types with physical dimension
Date: Wed, 28 Sep 1994 18:56:55 GMT
Date: 1994-09-28T18:56:55+00:00 [thread overview]
Message-ID: <1994Sep28.185655.23074@wdl.loral.com> (raw)
In-Reply-To: INFO-ADA%94092805521945@VM1.NODAK.EDU
Another possibility is to use a discriminated type to keep track of dimensions
like so:
generic
type Base is digits <>;
package Units is
type Unit(Length_DIM: Integer; Mass_DIM: Integer; Time_DIM: Integer) is
private;
function "*"(Left, Right: Unit) return Unit;
function "*"(Left: Unit; Right: Base) return Unit;
function "+"(Left, Right: Unit) return Unit;
function "-"(X: Unit) return Unit;
-- etc;
subtype Length is Unit(1,0,0);
subtype Area is Unit(2,0,0);
subtype Mass is Unit(0,1,0);
subtype Time is Unit(0,0,1);
suntype Frequency is Unit(0,0,-1);
subtype Energy is Unit(2,1,-2);
-- etc;
private
type Unit(Length_DIM: Integer; Mass_DIM: Integer; Time_DIM: Integer) is
record
Value: Base;
end record;
end Units;
package body Units is
procedure Check_DIM(Left, Right: Unit) is
begin
if Left.Length_DIM \= Right.Length_DIM or else
Left.Mass_DIM \= Right.Mass_DIM or else
Left.Time_DIM \= Right.Time_DIM then
raise CONSTRAINT_ERROR;
end Check_DIM;
function "*"(Left, Right: Unit) return Unit is
begin
return Unit'(Left.Length_DIM+Right.Length_DIM,
Left.Mass_DIM+Right.Mass DIM,
Left.Time_DIM+Right.Time_DIM,
Left.Value * Right.Value);
end "*";
function "+"(Left, RIght: Unit) return Unit is
begin
Check_DIM(Keft, Right);
return UniL'(Left.Length_DIM,
Left.Mass_DIM,
Left.Time_DIM,
Left.Value + Right.Value);
end "+";
function "-"(X: Unit) is
begin
return Unit'(X.Length_DIM,
X.Mass_DIM,
X.Time_DIM,
-X.Value);
end "-";
-- etc;
end Units;
Yes I know that this requires runtime dimension checking, but you only
have to use it until you are through debugging your code then
you can replace the body with one that turns off all the checking.
--
Mark Biggar
mab@wdl.loral.com
next prev parent reply other threads:[~1994-09-28 18:56 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
1994-09-28 10:55 Types with physical dimension Simtel20 Transfer
1994-09-28 18:56 ` Mark A Biggar [this message]
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
-- strict thread matches above, loose matches on Subject: below --
1994-09-28 19:28 Paul Pukite
1994-09-27 22:18 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
1994-10-05 10:35 ` Stephen J Bevan
1994-10-05 13:17 ` Jean-Pierre Rosen
1994-10-05 15:48 ` Norman H. Cohen
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox