Boost logo

Boost :

From: Phil Richards (news_at_[hidden])
Date: 2003-10-24 03:09:54

On 2003-10-21, Andy Little <andy_at_[hidden]> wrote:
> "Paul A. Bristow" <boost_at_[hidden]> wrote in message
[wants all values stored internally as SI units]
> Within SI units there are various units within 1 dimension.
> ie length has units from nanometres to kilometres (and beyond).
[... lot of stuff I pretty agree with zapped ...]

Minor quibble: within "SI units", there is one, and only one unit
for each dimension.

There are multiple defined prefixes for units, but SI (base) defines
only the 7 basic units.

Anyway, this thread has gone on in so many different branches
I think I've started to lose visibility of what is being said
and why it is or isn't right.

On the project I work on, we built our own dimensional analysis
class. We are (currently) forced to use MSVC++6, and hence
can't do a lot of the neato template stuff that we would *really*
like to do.

At the simplest level, we have something like (names changed to
protect the innocent):

typedef Dimensions<double, 1,0,0,0,0,0,0> Time;
typedef Dimensions<double, 0,1,0,0,0,0,0> Distance;

And derived quantities like:
typedef Dimensions<double, -1,1,0,0,0,0,0> Speed;

We have the *convention* that these are storing SI based values.
It is nothing more than convention - the same library can be
used without change with any _consistent_ set of units. If you
want time in hours, and distance in miles, then your speeds
will be in miles per hour. The dimensional analysis checking
works just as well. What you *don't* have is conversions between
different units.

This suggests to me that units are orthogonal to dimensional analysis.
Which I think is what Andy is saying.

In fact I lied: we don't actually specify the first argument
as "double", we use a policy type, and use its value_type definition.
(We stick a lot of other stuff in there to generate fudge-factor
comparison operators that can be tuned to the domain we are working
in, but that's not important right now.)

This could probably be allow us to have parallel unit representations
if we so desired:

typedef Dimensions<SIPolicy, 0,1,0,0,0,0,0> SIDistance;
typedef Dimensions<ImperialPolicy, 0,1,0,0,0,0,0> ImperialDistance;

This allows:
SISpeed s = SIDistance(1.0) / SITime(1.0);

But prevents:
SIDistance sid = ImperialDistance(1.0);

*unless* an appropriate (set of) conversion operators is defined.
I'm pretty darned sure that the conversion operators could be
stamped out fairly trivially - especially if you stuck the conversion
factors to some common base (SI springs to mind) in the policy.

Hmmm. I think I'll try modifying our current code and see if it
works :-)

> Assuming the specialisations previously discussed, if you really need
> to use (say) feet and stay only with feet, in my system you will not
> get unit conversion errors,

Yup. This seems like a sensible and reasonable requirement for
a dimensional analysis library. My view is that unit conversion
should be on demand, not unavoidable just by using the library.


change name before "@" to "phil" for email
otherwise it will drop in my spam catcher

Boost list run by bdawes at, gregod at, cpdaniel at, john at