From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,791ecb084fdaba75 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-09-28 11:58:17 PST Newsgroups: comp.lang.ada Path: bga.com!news.sprintlink.net!howland.reston.ans.net!swrinde!elroy.jpl.nasa.gov!lll-winken.llnl.gov!enews.sgi.com!wdl1!dst17!mab From: mab@dst17.wdl.loral.com (Mark A Biggar) Subject: Re: Types with physical dimension Message-ID: <1994Sep28.185655.23074@wdl.loral.com> Sender: news@wdl.loral.com Organization: Loral Western Development Labs References: Date: Wed, 28 Sep 1994 18:56:55 GMT Date: 1994-09-28T18:56:55+00:00 List-Id: 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