Boost logo

Boost :

From: Jan Langer (jan_at_[hidden])
Date: 2003-10-15 17:38:11


Brock Peabody wrote:
>>>In other words, the library does not need to
>>>explicitly define units or dimensions for velocity, momentum, or energy.
>>>These would be automatically derived from the more fundamental
>>>dimensions (mass, length, time).
>>
>>I'm not sure how this would work outside the metric system, eg gallons,
>>or BTU ( http://www.sizes.com/units/british_thermal_unit.htm )
>
> If I understand it like I think I do, it would matter what system you used,
> but the library wouldn't be able to do anything to keep you from mixing two
> systems. I could see some utility in being able to say "this is 2 kg"
> instead of "this is 2 mass units" so you couldn't use kg and pounds
> together... or maybe you could and the system would 'know' what the
> difference is.

but you could define the base as the SI unit and everything what was
mentioned should work.
if meter³ is the basic volume unit and a gallon is defined as
0.00378*meter³ you can add quantities of type volume together as you
like. internally it is all meter³, but the user should not care.

i havent looked at the existing units packages, but i'd imagine
something like:

typedef ... length;
typedef ... volume;

length const meter (1);
length const dezimeter = meter / 10;
volume const meter_3 = pow <3> (meter);
volume const liter = pow <3> (dezimeter); // or: meter_3 / 1000
volume const gallon = 3.78 liter;

volume car_tank = 30 * gallon + 10 * liter + meter_3;

a boost library should imho provide only different packages of constants
like:

namespace dimensions
{
        typedef list_c <int, 1, 0, 0, ...> dim1;
        typedef list_c <int, 0, 1, 0, ...> dim2;
        typedef list_c <int, 0, 0, 1, ...> dim3;
        ...
};

// operators for dimensions as described in dave's slides
// both runtime and compile time

template <typename Dim1, typename Dim2>
struct addition
{
        // assert Dim1 equals Dim2
        typedef Dim1 type;
};

namespace physics
{
        typedef dimensions::dim1 length;
        typedef dimensions::dim2 mass;
        ...
        typedef multiplication <length, length>::type area;
        typedef multiplication <area, length>::type volume;
};

template <typename T>
struct si
{
        static quantity <T, physics::length> const meter; // value = 1

        // meter * meter * meter
        static quantity <T, physics::volume> const meter_3;
};

template <typename T>
struct other_units
{
        static quantity <T, physics::volume> const gallon;
                // = 0.00378 * si <T>::meter_3;
};

ok, the pseudo-code got a little out of hand. but i just dont understand
the confusion here. there are of course a lot of problems like type
promotion, fractional dimensions and so on but everyone who needs a
different unit system can build it on top of either dimensions, physics
or special units.

jan

-- 
jan langer ... jan_at_[hidden]
"pi ist genau drei"

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