Boost logo

Boost Users :

Subject: Re: [Boost-users] units
From: Robert Ramey (ramey_at_[hidden])
Date: 2013-09-22 02:38:36


Nathan Crookston wrote:
> Hi Robert,
>
> It's a little confusing

it refers to my test case or the units library?

> 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:

... thanks for this i appreciate you'r going to this effort.

...

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

I concede that I don't get the "system" concept. It's hardly mentioned
in the documentation. I sort of had the feeling that all the units
had to be part of the same "system" in order to automatically
converted.

> typedef unit<
> consumption_rate_dimension,
> consumption_system
>> consumption_rate_unit;

> typedef quantity<consumption_rate_unit, float> consumption_rate;

hmmm - so this doesn't use the header ?
#include <boost/units/base_units/us/gallon.hpp>

> 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;
> }

OK - but now how would I output in meters^3/second ? It seems that the
output is coupled to the way the unit is setup. I would have expected
that the format of the output display could be delayed until the actual
output operation is performed - just like i can on input.

that is, Since I can do (using si)
consumption_rate cr = static_cast<consumption_rate>( 24.0 * gallons / hour)

I expect to be able to do some similar operation equivalent to
static_cast<gallons/hour>(r) - OK I suppose that some sort of symetical
operation is not implementable. But on the other hand, the following does
work

cout << 24.0 gallons / hour.

Thanks for your help.

Robert Ramey


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