Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!samsung!think!mintaka!ogicse!decwrl!megatest!djones From: djones@megatest.UUCP (Dave Jones) Newsgroups: comp.lang.misc Subject: Re: Typing on units Message-ID: <12354@goofy.megatest.UUCP> Date: 16 Mar 90 23:20:03 GMT References: <40346@ism780c.isc.com) Organization: Megatest Corporation, San Jose, Ca Lines: 34 From article <40346@ism780c.isc.com), by news@ism780c.isc.com (News system): ) In article <883@enea.se) sommar@enea.se (Erland Sommarskog) writes: ))Marvin Rubenstein (marv@ism780.UUCP) writes: ) I (Marv Rubinstein) talked about using units for an aid in error ) detection. ... ) The language I had in mind was more like this. ) ) units ) m -- mass ) l -- length ) t -- time ) f = m*l/(t*t) -- force ) end units I think the catch-phrase here is 'dimensional analysis'. It's not hard to do. I've implemented it in a Pascal superset. It can be done by the compiler if things are simple enough, or at runtime otherwise. You could do a pretty good job of it in C++ without touching the compiler, relying on runtime checks, of course. The trick is to keep, along with each value, an array of integers, one for each dimension, e.g. mass, length, time, force. When values are multiplied, their dimensions are added. Get it? For example, pure 'length' would have a dimension of (0,1,0,0) -- a one in the length-dimension. 'time' would have a dimension of (0,0,1,0) -- a one in the time dimension. 'length/time' would have the dimension (0,1,-1,0) -- the dimension of length _minus_ the dimension of time. Because division is the inverse of multiplication, the time dimension is _subtracted_. I think you can work out the rest.