gpt4 book ai didi

c++ - boost.units 中单位的转换因子

转载 作者:行者123 更新时间:2023-11-30 05:09:10 25 4
gpt4 key购买 nike

我刚开始使用 boost-units,我想了解我应该编写什么代码来获取单位之间的转换因子。关注Runtime Units Example ,我设法得到了我需要的 base_units。

例如,对于长度,我可以很容易地得到从英寸到米的换算系数,如下所示:

conversion_factor(imperial::inch_base_unit::unit_type(), si::meter_base_unit::unit_type())

这最终允许我定义任意数量的缩放单位,并获得所有需要的转换因子。

例如,在相关 header 中声明为“meter_per_second”单位的速度的情况下,我不知道如何检索转换因子以将其转换为 kmh 或 mph。我想我需要定义自己的 mph 单位,这导致需要定义(或检索)英里和小时的定义,并将它们放在一起。

我应该如何达到预期的结果?

最佳答案

我设法在 Boost-users 邮件列表存档 (here) 中找到了相关示例

#include <boost/units/base_units/us/mile.hpp>
#include <boost/units/base_units/metric/hour.hpp>

typedef boost::units::us::mile_base_unit::unit_type mile_unit;
typedef boost::units::metric::hour_base_unit::unit_type hour_unit;

typedef boost::units::divide_typeof_helper<mile_unit, hour_unit>::type miles_per_hour;

一旦正确声明了单位,就可以调用conversion_factor 函数来获取我需要的内容。

关于c++ - boost.units 中单位的转换因子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46326816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com