Boost logo

Boost Users :

Subject: [Boost-users] [boost][units]: Looking for more elegant ways to define RPM and velocity[km/h]
From: Dennis Gertitschke (dennis.gertitschke_at_[hidden])
Date: 2013-03-22 04:13:21


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)?
b) Is the cast to quantity<frequency> in velocity calculation avoidable, e.g. can be done automatically?

Thanks in advance.

Dennis

#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/io.hpp>
#include <boost/units/systems/si/prefixes.hpp>
#include <boost/units/base_units/metric/minute.hpp>

using namespace boost::units;
using namespace boost::units::si;
using namespace boost::units::metric;
using namespace std;

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

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

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

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