How to add your own base unit and conversions using boost::units
Asked Answered
B

1

10

I am currently using boost::units to represent torque in si units, however I am given the torque in pound feet. I am attempting thus to create a pound_foot unit of torque and a conversion to support this. My lazy attempt was to simply define:

BOOST_STATIC_CONST(boost::si::torque, pound_feet = 1.3558179483314 * si::newton_meters);

And then do:

boost::si::torque torque = some_value * pound_feet;

But this feels unsatisfactory. My second attempt was to attempt to define a new base unit called a pound_foot (see below). But when I attempt to use it in a way similar to the above (with a cast to the si unit) I get a page full of errors. Any suggestions on the correct approach?

namespace us {
  struct pound_foot_base_unit : base_unit<pound_foot_base_unit, torque_dimension> { };
    typedef units::make_system<
            pound_foot_base_unit>::type us_system;
    typedef unit<torque_dimension, us_system> torque;
    BOOST_UNITS_STATIC_CONSTANT(pound_foot, torque);
    BOOST_UNITS_STATIC_CONSTANT(pound_feet, torque);        
}
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(us::torque, 
                                     boost::units::si::torque, 
                                     double, 1.3558179483314);
Beeswing answered 3/10, 2011 at 20:4 Comment(0)
L
9

Pound foot isn't really a base unit, so better go with the clean way and define it in terms of the base units, which are feet and pounds:

#include <boost/units/base_units/us/pound_force.hpp>
#include <boost/units/base_units/us/foot.hpp>
#include <boost/units/systems/si/torque.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/io.hpp>
#include <iostream>

namespace boost {
namespace units {
namespace us {

typedef make_system< foot_base_unit, pound_force_base_unit >::type system;
typedef unit< torque_dimension, system > torque;

BOOST_UNITS_STATIC_CONSTANT(pound_feet,torque);

}
}
}

using namespace boost::units;

int main() {
    quantity< us::torque > colonial_measurement( 1.0 * us::pound_feet );
    std::cerr << quantity< si::torque >(colonial_measurement) << std::endl;
    return 0;
}

This program computes the conversion factor from the known values of foot and pound, the output is 1.35582 m^2 kg s^-2 rad^-1. Please allow me nonetheless to sneer at the inferiority of the imperial system.

Liaoning answered 3/10, 2011 at 21:13 Comment(1)
Why would you sneer at the imperial system? It makes so much sense, sometimes you have 12 units per sub-unit, sometimes 60, sometimes 3. and the names for prefixes are different between types of things. Its a great system and your sneering is not appreciated.Yingling

© 2022 - 2024 — McMap. All rights reserved.