Boost logo

Boost :

From: Ross MacGregor (ross__macgregor_at_[hidden])
Date: 2003-10-25 18:38:10

Hugo Duncan wrote:
> Hugo Duncan <hugoduncan_at_[hidden]> wrote:
> //! definition of length dimension (integers for exposition)
> typedef mpl::vector< 1, 0, 0, 0, 0, 0, 0> length;
> //! definition of meters and millimeters as units of length
> struct meter { typedef length dimension; };
> struct millimeter { typedef length dimension; };
> //! definition of quantity
> typedef quantity<double, meter> meters; //< value stored in meters
> typedef quantity<double, millimeter> millimeters; //< value stored in
> millimeters
> Trying to encode the conversion factors into the type signature
> seems ugly to me. Unit conversion takes place from one unit to another
> and this can be expressed directly (untested code below).

Would this lead to an explosion of converters?

My original idea was each unit would define its relationship to the
standard SI unit. Therefore the transform algorithm will need to work
both ways:

meter -> T() -> kilometer
kilometer -> -T() -> meter

Convertion between two units would then require a two step precess.

kilometer -> -T() -> meter -> T() -> centimeter

This simple approach saves us from a converter explosion but has it's

1) Two step process.
2) Possible unneccessary precision reduction. This may happen when
converting between two units of similar scale. For example when
converting lightyears to AUs, the intermediate step of converting to
meters may result in a loss of precision.

Now if this is unacceptable, do you suppose we could matematically
reduce these two operations to a single one?

What if this was the default behavior and custom converters could be
written to override the default mechanism.

I suppose on a per-application basis that setting up converters would
not be so bad if it was an easy one liner.

Converter<AU,scale<1,6,-5>,Lightyear> AU_to_Lightyear;
Converter<Lightyear,scale<6,3239249311,4>,AU> Lightyear_to_AU;

// Or if every transform entity also defined its inverse
// we would ony need one declaration
Converter<AU,scale<1,6,-5>,Lightyear> AU_to_Lightyear;

I have no idea how this would work, what am I going to do with
Lightyear_to_AU?? I hope you don't mind me throwing out ideas without
lots of solutions. I find this an intriguing subject.

Ross MacGregor

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