Boost logo

Boost Users :

Subject: Re: [Boost-users] units
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2013-09-22 00:15:30


Hi Robert,

Robert Ramey wrote:
> I have a program in which there is the notion of consumption rate such as
> m^3/sec, gallons/hour, etc.
>
> I have consumption_rate unit defined in terms of si units. I read in a
> quanity in gallons/hour and so far so good.
> If I display the quantity it is displayed as m^3/sec. This displays the
> "compound units" as expected - very cool.
> All this is OK as far as it goes. When I try to display the quantity as
> gallons/hour again I have a whole host of
> problems. The attached test fill illustrates the issues. The only way I've
> been able to do is is by making
> a complete end run around the units system itself - sort of self defeating.
>
> I would appreciate insight anyone might be able to offer into this issue.

It's a little confusing, but it's printing in the si units because
consumption_rate_unit is generated from the si::system. You can
create your own system with volume and time and create the consumption
rate from it. The following shows how:

#include <iostream>

#include <boost/units/io.hpp>
#include <boost/units/pow.hpp>
#include <boost/units/systems/cgs.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/io.hpp>

#include <boost/units/base_units/metric/hour.hpp>
#include <boost/units/base_units/us/gallon.hpp>

using namespace boost::units;

// derive special units that we use in our application
// derived dimension for consumption_rate : M T^-1
typedef boost::units::derived_dimension<
    boost::units::length_base_dimension,3,
    boost::units::time_base_dimension,-1
>::type consumption_rate_dimension;

typedef boost::units::make_system<us::gallon_base_unit,
metric::hour_base_unit>::type consumption_system;

typedef unit<
    consumption_rate_dimension,
    consumption_system
> consumption_rate_unit;

typedef quantity<consumption_rate_unit, float> consumption_rate;

typedef unit<volume_dimension, consumption_system> gallon_unit;
BOOST_UNITS_STATIC_CONSTANT(gallon, gallon_unit);
BOOST_UNITS_STATIC_CONSTANT(gallons, gallon_unit);

typedef unit<time_dimension, consumption_system> hour_unit;
BOOST_UNITS_STATIC_CONSTANT(hours, hour_unit);
BOOST_UNITS_STATIC_CONSTANT(hour, hour_unit);

int main()
{
    quantity<consumption_rate_unit> C(1.0 * gallon / hour);

    std::cout << 1.0 * gallons / hour << " - ok displays gal/hour" << std::endl;
    std::cout << C << " - ok displays gal/hour" << std::endl ;

    return 0;
}

HTH,
Nate


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net