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-10-05 04:11:54 PST Path: bga.com!news.sprintlink.net!howland.reston.ans.net!math.ohio-state.edu!jussieu.fr!news-rocq.inria.fr!news2.fnet.fr!enst!not-for-mail From: rosen@enst.fr (Jean-Pierre Rosen) Newsgroups: comp.lang.ada Subject: Re: Types with physical dimension Date: 5 Oct 1994 09:38:05 +0100 Organization: Ecole Nationale Superieure des Telecommunications, Paris France Message-ID: <36tole$j5e@cyclope.enst.fr> References: NNTP-Posting-Host: cyclope.enst.fr Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Date: 1994-10-05T09:38:05+01:00 List-Id: In article , 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?