【问题标题】:How to use Boost.Unit with ratios in the same unit如何使用具有相同单位比率的 Boost.Unit
【发布时间】:2017-11-02 04:33:28
【问题描述】:

我有一个使用 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&lt;10, static_rational&lt;3&gt; 还是已经定义并可用?

【问题讨论】:

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


    【解决方案1】:

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

    虽然此代码不涉及频率,但您应该能够根据需要对其进行调整(例如,有一个 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),但在某些时候这是要求一个明确的演员表。

    理论上,您不需要在第二行中手动将米转换为毫米,但明显的构造 x / millimeters 会产生编译错误,而我从未设法找到一个好的解决方法(比例尺不不要像它应该的那样取消)。

    (您也可以使用x.value() 而不是x / meters,但我不喜欢这种方法,因为如果 x 的基本单位不是您所期望的,它仍然会编译并给您带来令人惊讶的结果。而且它仍然没有解决毫米转换问题。)

    您也可以考虑使用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;
    

    【讨论】:

    • 看来我仍然需要定义自己的单位和数量,但我至少可以重用前缀?我仍然希望变量采用不同的单位,而不是全部采用相同的赫兹单位,但有些采用千赫兹等。
    • 我添加了另一个示例,可以更直接地回答您关于频率和多种数量类型的问题。
    猜你喜欢
    • 2017-12-17
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多