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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,1dd28d5040ded1f8 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-05-14 11:51:50 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!news.gtei.net!news-out.visi.com!hermes.visi.com!uunet!ash.uu.net!spool0900.news.uu.net!reader0902.news.uu.net!not-for-mail Message-ID: <3CE15D0A.3050100@mail.com> Date: Tue, 14 May 2002 14:52:58 -0400 From: Hyman Rosen User-Agent: Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.0rc1) Gecko/20020417 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Announce: Grace project site operational References: <4519e058.0205140718.3ddb1b2a@posting.google.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Organization: KBC Financial Products Cache-Post-Path: master.nyc.kbcfp.com!unknown@mosquito.nyc.kbcfp.com X-Cache: nntpcache 2.3.3 (see http://www.nntpcache.org/) NNTP-Posting-Host: 204.253.250.10 X-Trace: 1021402309 reader2.ash.ops.us.uu.net 26392 204.253.250.10 Xref: archiver1.google.com comp.lang.ada:24050 Date: 2002-05-14T14:52:58-04:00 List-Id: Preben Randhol wrote: >>As a chemist I want mol and Kelvin, but how do one deal with >>constructions like this: >> >>Constant = (C1*C2)/(C3^0.5) = mol�/mol^0.5 = mol^(3/2) Time for me to bring up my usual post about units in C++, which can be done with complete type safety and with no run-time overhead in time or storage, because of C++'s automatic instantiation rules. Here's the simplified example involving only mass, distance, and time. The code allows only like units to be added or subtracted, but arbitrary units to be multiplied or divided, with the correctly typed result. Constants are units with all paremeters set to zero. All the verbiage is for types; the actual storage for each unit object is just the value, and all type correctness is checked at compile time, not run time. The correctly typed operators are automatically instantiated when used, without manual intervention. I first saw this idiom in _Scientific and Engineering C++_ by Bartion & Nackman. template struct Unit { RepType value; explicit Unit(value) : value(value) { } Unit operator+(Unit other) { return Unit(value + other.value); } Unit operator-(Unit other) { return Unit(value - other.value); } }; template Unit operator*(Unit l, Unit r) { return Unit(l.value * r.value); } template Unit operator/(Unit l, Unit r) { return Unit(l.value / r.value); } typedef Unit Velocity; typedef Unit Acceleration; typedef Unit Energy; typedef Unit Herz; // etc.