From: Hyman Rosen <hyrosen@mail.com>
Subject: Re: Expressing physical units (Was: Reprise: 'in out' parameters for functions)
Date: Sun, 11 Apr 2004 02:45:34 GMT
Date: 2004-04-11T02:45:34+00:00 [thread overview]
Message-ID: <ij2ec.22694$1y1.20221@nwrdny03.gnilink.net> (raw)
In-Reply-To: <pllll4djox.fsf@sparre.crs4.it>
Jacob Sparre Andersen wrote:
> It should be noted that my analysis was tied rather strongly to Ada,
> and a more relaxed programming language might be able to do something
> that would be sufficiently close to full compile-time unit checking.
C++ can do full compile-time unit checking with no runtime overhead
as long as all units needed by the program are known at compile-time.
The technique uses templates, requires automatic instantiation of
templates for usability (and so cannot be done in Ada), and was published
in Barton & Nackman, _Scientific and Engineering C++_. I've often posted
the technique, but here goes again, in a simplified version:
template <int Mass, int Distance, int Time>
struct Unit
{
double value;
Unit(double value = 0.0) : value(value) { }
Unit operator+(Unit other) { return Unit(value + other.value); }
};
template <int M1, int D1, int T1,
int M2, int D2, int T2>
Unit<M1+M2,D1+D2,T1+T2>
operator*(Unit<M1,D1,T1> l, Unit<M2,D2,T2> r)
{
return Unit<M1+M2,D1+D2,T1+T2>(l.value * r.value);
}
This supports positive and negative whole number powers. For those rare
applications which require fractional powers, you can move to using pairs
of numerator/denominator template parameters to represent fractions.
The full implementation will include the other operators, and may have the
value type as a template parameter as well, but this is enough of a guide
to see how it works.
Here's how you might write the output operator:
template <typename OStream, int M, int D, int T>
Ostream &operator<<(Ostream &o, const Unit<M, D, T> &v)
{
o << v.value;
if (M > 0) o << " g";
if (M > 1) o << "^" << M;
if (D > 0) o << " cm";
if (D > 1) o << "^" << D;
if (T > 0) o << " s";
if (T > 1) o << "^" << T;
if (M < 0 || D < 0 || T < 0) o << " /";
if (M < 0) o << " g";
if (M < 1) o << "^" << -M;
if (D < 0) o << " cm";
if (D < 1) o << "^" << -D;
if (T < 0) o << " s";
if (T < 1) o << "^" << -T;
return o;
}
Because M, D, T are template parameters, their values are all known at
compile time, and the output routine instantiated for a particular unit
will contain only the code needed, without any tests at runtime.
next prev parent reply other threads:[~2004-04-11 2:45 UTC|newest]
Thread overview: 81+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <u4qrv5hwr.fsf@acm.org>
2004-04-08 17:19 ` Reprise: 'in out' parameters for functions Alexander E. Kopilovich
[not found] ` <bRecOT0TxF@VB1162.spb.edu>
2004-04-08 23:46 ` Stephen Leake
2004-04-09 9:23 ` Florian Weimer
2004-04-09 10:04 ` Dmitry A. Kazakov
2004-04-09 11:23 ` Martin Krischik
2004-04-09 12:44 ` Dmitry A. Kazakov
2004-04-09 22:48 ` Randy Brukardt
2004-04-14 14:40 ` Robert I. Eachus
2004-04-14 21:20 ` Randy Brukardt
2004-04-09 22:47 ` Florian Weimer
2004-04-10 10:49 ` Dmitry A. Kazakov
2004-04-10 11:11 ` Florian Weimer
2004-04-10 13:26 ` Dmitry A. Kazakov
2004-04-10 20:50 ` Georg Bauhaus
2004-04-11 10:31 ` Dmitry A. Kazakov
2004-04-09 11:27 ` Stephen Leake
2004-04-09 22:46 ` Randy Brukardt
2004-04-09 13:12 ` Wojtek Narczynski
2004-04-09 15:48 ` Expressing physical units (Was: Reprise: 'in out' parameters for functions) Jacob Sparre Andersen
2004-04-10 13:07 ` Wojtek Narczynski
2004-04-10 13:52 ` Jacob Sparre Andersen
2004-04-11 2:45 ` Hyman Rosen [this message]
2004-04-11 10:14 ` Expressing physical units Jacob Sparre Andersen
2004-04-11 16:05 ` Hyman Rosen
2004-04-12 6:58 ` Expressing physical units (Was: Reprise: 'in out' parameters for functions) Russ
2004-04-12 10:29 ` Dmitry A. Kazakov
2004-04-13 6:52 ` Russ
2004-04-13 10:55 ` Dmitry A. Kazakov
2004-04-14 4:50 ` Hyman Rosen
2004-04-14 8:49 ` Dmitry A. Kazakov
2004-04-14 16:49 ` Hyman Rosen
2004-04-15 10:37 ` Dmitry A. Kazakov
2004-04-14 7:10 ` Russ
2004-04-14 8:53 ` tmoran
2004-04-14 9:01 ` Vinzent 'Gadget' Hoefler
2004-04-14 9:21 ` Dmitry A. Kazakov
2004-04-13 9:53 ` Wojtek Narczynski
2004-04-15 21:27 ` Expressing physical units Jacob Sparre Andersen
2004-04-16 11:40 ` Dmitry A. Kazakov
2004-04-09 16:17 ` Reprise: 'in out' parameters for functions Georg Bauhaus
2004-04-10 2:28 ` Wojtek Narczynski
2004-04-10 9:46 ` Georg Bauhaus
2004-04-10 10:49 ` Dmitry A. Kazakov
2004-04-10 15:35 ` Wojtek Narczynski
2004-04-10 21:01 ` Georg Bauhaus
2004-04-10 21:16 ` Georg Bauhaus
2004-04-11 13:20 ` exception parameters Stephen Leake
2004-04-12 10:29 ` Dmitry A. Kazakov
2004-04-13 0:58 ` Stephen Leake
2004-04-13 1:30 ` Randy Brukardt
2004-04-13 8:04 ` Jean-Pierre Rosen
2004-04-11 10:31 ` Reprise: 'in out' parameters for functions Dmitry A. Kazakov
2004-04-12 22:02 ` Randy Brukardt
2004-04-13 10:56 ` Dmitry A. Kazakov
2004-04-14 21:12 ` Randy Brukardt
2004-04-15 10:37 ` Dmitry A. Kazakov
2004-04-13 9:30 ` Wojtek Narczynski
2004-04-13 12:00 ` Dmitry A. Kazakov
2004-04-13 22:41 ` Wojtek Narczynski
2004-04-14 8:49 ` Dmitry A. Kazakov
2004-04-14 15:03 ` Wojtek Narczynski
2004-04-15 10:37 ` Dmitry A. Kazakov
2004-04-16 0:29 ` Wojtek Narczynski
2004-04-16 11:36 ` Dmitry A. Kazakov
2004-04-16 19:25 ` Wojtek Narczynski
2004-04-14 15:57 ` Robert I. Eachus
2004-04-15 8:04 ` Dmitry A. Kazakov
2004-04-10 12:32 ` Wojtek Narczynski
2004-04-14 15:46 ` Robert I. Eachus
2004-04-16 1:52 ` Wojtek Narczynski
2004-04-16 5:40 ` Robert I. Eachus
2004-04-16 11:38 ` Wojtek Narczynski
2004-04-16 16:30 ` Robert I. Eachus
2004-04-16 18:38 ` Randy Brukardt
2004-04-16 22:15 ` Wojtek Narczynski
2004-04-17 1:20 ` Robert I. Eachus
2004-04-17 11:42 ` Wojtek Narczynski
2004-04-17 14:14 ` Robert I. Eachus
2004-04-16 19:28 ` Wojtek Narczynski
2004-04-09 17:09 ` Pascal Obry
2004-04-10 2:37 ` Wojtek Narczynski
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox