Boost logo

Boost :

From: Hugo Duncan (hugoduncan_at_[hidden])
Date: 2003-10-24 17:39:16

Hugo Duncan <hugoduncan_at_[hidden]> wrote:
> quantity<NumericType, Unit, Dimension>

on second thoughts, that was not so well thought out. This
is somewhat better, providing a clean seperation of dimensions
and units.

   //! 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

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).

Here the converter class is defined in place, but
this interface could be forwarded to a simpler implementation (eg. just
defining the factor) or to you favourite existing units conversion

   template <>
   struct converter<millimeter, meter>
     inline double factor() const { return 0.001; }

     // unit power conversion
     template <typename BasicNumericType>
     static BasicNumericType convert(BasicNumericType value)
       return value*factor();

     // rational power conversion
     template <typename Rational, typename BasicNumericType>
     static BasicNumericType convert(BasicNumericType value)
       static f( std::pow(factor(), Rational::as_double()) );
       return value*f;

     // rational power conversion
     template <typename BasicNumericType>
     static BasicNumericType convert(BasicNumericType value, double power)
       return value*std::pow(factor(), power) );

Each unit would need a rank, so that the result unit of an aritmetic
could be determined

eg, so
    millimeters(3)*millimeters(3) == millimeters(9)
    millimeters(3)*meters(0.003) == meters(0.009)

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