如何使用Boost.Unit与相同单位的比率

时间:2017-06-01 06:57:20

标签: c++ c++11 boost boost-units

我有一个非常简单的使用Boost.Unit的用例,但不确定是否有更好/更简单的方法来完成相同的工作。

我想在相同的单位之间转换,但不同的比率。例如,赫兹到千赫兹到兆赫兹。

根据我的理解,我首先必须用我的特定比率来定义单位:

typedef boost::units::make_scaled_unit<si::frequency, scale<10, static_rational<0> > >::type  Hertz_unit;
typedef boost::units::make_scaled_unit<si::frequency, scale<10, static_rational<3> > >::type  KilloHertz_unit;
typedef boost::units::make_scaled_unit<si::frequency, scale<10, static_rational<6> > >::type  MegaHertz_unit;

然后创建代表单位的数量:

typedef boost::units::quantity<Hertz_unit     , double> Hertz;
typedef boost::units::quantity<KilloHertz_unit, double> KilloHertz;
typedef boost::units::quantity<MegaHertz_unit , double> MegaHertz;

最后一些常量和文字:

BOOST_UNITS_STATIC_CONSTANT( Hz, Hertz_unit     );
BOOST_UNITS_STATIC_CONSTANT(KHz, KilloHertz_unit);
BOOST_UNITS_STATIC_CONSTANT(MHz, MegaHertz_unit );

Hertz      operator"" _Hz  (long double val) { return Hertz     (val *  Hz); }
KilloHertz operator"" _KHz (long double val) { return KilloHertz(val * KHz); }
MegaHertz  operator"" _MHz (long double val) { return MegaHertz (val * MHz); }

现在我可以使用数量:

Hertz      freq_1 = (10 *  Hz);
KilloHertz freq_2 = (10 * KHz);
MegaHertz  freq_3 = (10 * MHz);
// OR
Hertz      freq_4 = 10.0_Hz;
KilloHertz freq_5 = 10.0_KHz;
MegaHertz  freq_6 = 10.0_MHz;
// Convert between units
Hertz freq_7 = static_cast<Hertz>(10 * KHz);

这是应该如何使用Boost.Unit还是我错过了一些可能更容易使用的东西?

我是否可以在标题中隐藏的地方使用已定义的单位/数量?或者我应该为我使用的所有单位做到这一点?

我是否需要知道/记住Kilo是scale<10, static_rational<3>还是已经定义并且可用?

1 个答案:

答案 0 :(得分:2)

有一些不同的预定义“系统”可以使事情更容易使用,并且避免需要定义自己的单位和比例。

虽然此代码不涉及频率,但您应该能够根据需要进行调整(例如,有一个boost/units/systems/si/frequency.hpp标题):

#include <boost/units/quantity.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/prefixes.hpp>

using boost::units::si::meters;
using boost::units::si::milli;

typedef boost::units::quantity<boost::units::si::length> length;
static const auto millimeters = milli * meters;

// ...

auto x = length(5 * millimeters);
auto mm = double(x / meters * 1000.0);

您曾经能够在没有明确强制转换为length的情况下执行此操作(尽管您需要将变量明确键入length而不是使用auto),但在某些情况下这是为了要求一个明确的演员。

理论上你不需要在第二行手动从米转换到mm,但明显的构造x / millimeters会产生编译错误,我从来没有设法找出一个好的解决方法(规模)不会像它应该的那样取消。)

(你也可以使用x.value()而不是x / meters,但我不喜欢这种方法,因为如果x的基本单位不是你的话,它仍会编译并给你惊人的结果期待。它仍然无法解决mm转换问题。)

或者您可能需要考虑类似this answer的内容,尽管这主要是为了使用单个替代比例作为您的基本单位。

这是使用频率和多种数量类型的另一种方法:

#include <boost/units/quantity.hpp>
#include <boost/units/systems/si/frequency.hpp>
#include <boost/units/systems/si/prefixes.hpp>

using boost::units::si::hertz;
using boost::units::si::kilo;
using boost::units::si::mega;

static const auto kilohertz = kilo * hertz;
static const auto megahertz = mega * hertz;

typedef boost::units::quantity<boost::units::si::frequency> Hertz;
typedef boost::units::quantity<decltype(kilohertz)> KiloHertz;
typedef boost::units::quantity<decltype(megahertz)> MegaHertz;

// ...

auto freq_1 = Hertz(10 * hertz);
auto freq_2 = KiloHertz(10 * kilohertz);
auto freq_3 = MegaHertz(10 * megahertz);
auto freq_4 = KiloHertz(freq_3);
// freq1.value() == 10.0
// freq2.value() == 10.0
// freq3.value() == 10.0
// freq4.value() == 10000.0

你可以很容易地对这些做转换和数学;在这种情况下,value()可能是最有用的,因为它会自然地表达与变量相同的单位。

一个稍微不幸的行为是这些单位的默认字符串输出表示为倒数秒(例如,freq2是“10 k(s ^ -1)”)。所以你可能只是想避免使用它们。

是的,operator""也有效,所以您可以在上面替换它们:

Hertz     operator"" _Hz(long double val) { return Hertz(val * hertz); }
KiloHertz operator"" _kHz(long double val) { return KiloHertz(val * kilohertz); }
MegaHertz operator"" _MHz(long double val) { return MegaHertz(val * megahertz); }

auto freq_1 = 10.0_Hz;
auto freq_2 = 10.0_kHz;
auto freq_3 = 10.0_MHz;
auto freq_4 = KiloHertz(freq_3);

为了保持一致性,您还可以用Hertz来定义hertz,但由于怪癖,它比其他人更棘手;这虽然有效:

typedef boost::units::quantity<std::decay_t<decltype(hertz)>> Hertz;
typedef boost::units::quantity<std::decay_t<decltype(kilohertz)>> KiloHertz;
typedef boost::units::quantity<std::decay_t<decltype(megahertz)>> MegaHertz;