Boost logo

Boost :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2006-03-10 08:49:16


> -----Original Message-----
> From: boost-bounces_at_[hidden]
> [mailto:boost-bounces_at_[hidden]] On Behalf Of Andy Little

> > This (my small sample code) is in no way supposed to be a
> full-blown
> > library, BTW. It is just for amusement.
>
> How about rational dimension elements rather than integers.
> Would that be possible using the preprocessor?

The preprocessor isn't doing the work. It is just generating the code that does
the work. Use of the preprocessor (here) is just making all that code write
itself based on an initial sequence of fundamental units.

> This occurs
> rarely but needs to be possible.
> One example is in measuring electrical noise where units for
> noise-voltage density are Volts * (Hertz to_power -1/2).
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Out of curiosity, how does this occur starting from SI's fundamental units?

> The time element of the dimension works out as being to power
> -(2 1/2) FWIW!

You can certainly implement a static rational with a little template
metaprogramming. Reducing rational values with template metaprogramming would
be expensive in general, but here were mostly talking about very "small"
rationals. You'd just end up with two "fields" per dimension (numerator and
denominator) instead of one, and you'd have to use them as rational values.
E.g. the way that I wrote it, you basically have:

dim<mass, time, ...>

...where 'mass' and 'time' are integers. Instead, you have to have 'mass' and
'time' be types that represent rational values (such as 'rational<-5, 2>').

> > The things that I find amusing in it are that dimensions
> are specified
> > using variables, not types (somewhat unusual), and the way that the
> > reconstruction of type from variables occurs.
>
> Is that the same as how Boost.typeof works ...?
>
> acceleration a;
> mass m;
>
> BOOST_TYPEOF( m * a) f;
> //or
> BOOST_AUTO(f, m * a);
>
> regards
> Andy little

It is probably similar (as far as reconstruction goes). I think that specifying
dimensions using variables instead of types is different. Instead of the above,
you have something like:

// fundamentals:

<unspecified-type> length;
<unspecified-type> time;
<unspecified-type> mass;
// etc.

// composites:

DIM(length / time) velocity;
DIM(velocity / time) acceleration;
  // or DIM(length / (time * time))

DIM(mass * acceleration) force;
  // or DIM(mass * length / (time * time))

All of the above are variable declarations, not typedefs. They are variables
that have no function except to be used in expressions for the purpose of
extracting type. In a scheme like this, the user never even sees the actual
types of these variables. Instead, the type is 'named' via an expression.

Using this scheme, however, you can't say something like:

2 * time

So you'd have to say something like:

r<2> * time

(or r<1, 2> to get such a fractional exponent at all).

All of this is slightly more complex that what I wrote (which is no where near
being a full-fledged physical quantities library), but it is all possible.

Regards,
Paul Mensonides


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk