c++ - 如何在同一单位中使用具有比率的 Boost.Unit

标签 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> 吗?还是已经定义并可用?

最佳答案

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

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

#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 的基本单位不是您所期望的结果。它仍然没有解决 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;

关于c++ - 如何在同一单位中使用具有比率的 Boost.Unit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44300549/

相关文章:

c++ - 自动和复制省略

c++ - 在方法中更改类的私有(private)值而不将更改返回给 main()

c++ - 我可以在 boost::geometry 的多边形内存储附加信息吗?

c++ - 使用 std::unique_ptr 时 Qt Widget 不显示

c++ - C++中不同派生类的高效容器

c++ - 如何打印 HKEY 或将其转换为字符串?

c++ - CMake 不适用于 OSX 上的 C++11

c++ - 私有(private)类成员延迟初始化的最佳实践

c++ - 将 boost cpp_int 用于 pow() 和 rand() 等函数

c++ - boost::interprocess vector 不释放共享内存