如何使用boost::units添加自己的基本单位和转换

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

本文关键字:本单位 转换 自己的 添加 何使用 boost units      更新时间:2023-10-16

我目前使用boost::units来表示si单位的扭矩,但是我得到了磅英尺的扭矩。因此,我试图创建一个磅英尺单位的扭矩和转换,以支持这一点。我的懒惰尝试是简单地定义:

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

然后做:

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

但这感觉不令人满意。我的第二次尝试是尝试定义一个名为pound_foot的新基本单位(见下文)。但是,当我试图以类似于上面的方式使用它(转换为si单位)时,我得到了一个充满错误的页面。对正确的方法有什么建议吗?

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

磅英尺并不是一个真正的基本单位,所以最好采用简洁的方式,用基本单位来定义它,即英尺和磅:

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

该程序从已知的英尺和磅值计算转换系数,输出为1.35582 m^2 kg s^-2 rad^-1。尽管如此,请允许我嘲笑一下帝制的劣等性。