Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost][units]: Looking for more elegant ways to define RPM and velocity[km/h]
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2013-03-22 11:16:53


AMDG

On 03/22/2013 01:13 AM, Dennis Gertitschke wrote:
> Hi community,
>
> while the code below works, i am not sure if it is the best/most elegant approach.
> Specifically,
>
> a) can i somehow reuse the minute_base_unit from SI
> (rpm=dimensionless/minute_base_unit)?

Yes.

> b) Is the cast to quantity<frequency> in velocity calculation avoidable, e.g.
> can be done automatically?

No. Conversions between different units are intentionally explicit.

>
> Dennis
> <snip>
>
> BOOST_UNITS_DEFINE_BASE_UNIT_WITH_CONVERSIONS(si, revolution, revolutions per
> minute", "rpm", 1.0/60.0, frequency, 1);
> typedef revolution_base_unit::unit_type revolution;
> BOOST_UNITS_STATIC_CONSTANT(rpm, revolution);
>

typedef decltype(si::dimensionless /
metric::minute_base_unit::unit_type()) revolution;

> BOOST_UNITS_DEFINE_BASE_UNIT_WITH_CONVERSIONS(si, velocity_kmh, "velocity",
> "km/h", 1.0/3.6, velocity, 2);
> typedef velocity_kmh_base_unit::unit_type velocity_kmh;
> BOOST_UNITS_STATIC_CONSTANT(kmh, velocity_kmh);
>

typedef decltype(si::kilometers / metric::hour_base_unit::unit_type())
velocity_kmh;

> int main(int argc, char* argv[])
> {
> quantity<velocity> v(0.0 * meter_per_second);
> quantity<length> r(0.63 * meter);
> quantity<dimensionless> i_HA(4.5 * si_dimensionless);
> quantity<dimensionless> i_G(1.0 * si_dimensionless);
> quantity<revolution> n(3000.0 * rpm);
> quantity<dimensionless> pi2(2.0 * M_PI * si_dimensionless);
>
> cout << n << endl;
> v = ((quantity<frequency>)n / (i_HA * i_G)) * pi2 * r;
> cout << v << endl;
> quantity<velocity_kmh> v2(v);
> cout << v2 << endl;
> return 0;
> }
>

In Christ,
Steven Watanabe


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