![](/img/trans.png)
[英]how to keep unit conversions in boost::units in single-precision floats?
[英]How to add your own base unit and conversions using boost::units
我目前正在使用boost :: units來表示si單位的扭矩,但是我給出了英尺扭矩。 我試圖創造一個pound_foot單位的扭矩和轉換來支持這一點。 我的懶惰嘗試只是簡單地定義:
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平方公尺s ^ -2rad ^ -1。 但請允許我嘲笑帝國制度的劣勢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.