分享

c – 使用Boost.Units定义百分比

 印度阿三17 2019-08-31

我想用Boost.Units实现一个百分比单位,这样无量纲数量(如比率)可以表示为百分比.我已成功实现了质量密度单位之间的转换,但同样不适用于无量纲单位.这是我的代码(假设使用命名空间boost :: units;):

//
// gram per milliliter (g mL^-1)
//
namespace my {
        struct gram_per_milliliter_base_unit :
                base_unit<gram_per_milliliter_base_unit, mass_density_dimension, 1>
        {
                static std::string name() {return "gram per milliliter";}
                static std::string symbol() {return "g mL^-1";}
        };
        typedef gram_per_milliliter_base_unit::unit_type gram_per_milliliter_unit;
        BOOST_UNITS_STATIC_CONSTANT(gram_per_milliliter, gram_per_milliliter_unit);
        BOOST_UNITS_STATIC_CONSTANT(grams_per_milliliter, gram_per_milliliter_unit);
}
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(
        my::gram_per_milliliter_base_unit, si::mass_density, double, 1.0e3
); // 1 g mL^-1 == 1e3 kg m^-3 (SI unit)
BOOST_UNITS_DEFAULT_CONVERSION(my::gram_per_milliliter_base_unit, si::mass_density);

//
// percentage (%)
//
namespace my {
        struct percent_base_unit :
                base_unit<percent_base_unit, dimensionless_type, 2>
        {
                static std::string name() {return "percent";}
                static std::string symbol() {return "%";}
        };
        typedef percent_base_unit::unit_type percent_unit;
        BOOST_UNITS_STATIC_CONSTANT(percent, percent_unit);
}
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(
        my::percent_base_unit, si::dimensionless, double, 1.0e-2
); // 1 % == 1e-2 (SI dimensionless unit)
BOOST_UNITS_DEFAULT_CONVERSION(my::percent_base_unit, si::dimensionless);

“每毫升克数”部分按预期工作:我可以编译此代码(假设使用命名空间my;以及):

quantity<gram_per_milliliter_unit> q1my(3*grams_per_milliliter);
quantity<si::mass_density> q1si(q1my);
quantity<gram_per_milliliter_unit> q1back(q1si);

但是以下两个转换都无法编译:

quantity<percent_unit> q2my(3*percent);
quantity<si::dimensionless> q2si(q2my);
quantity<percent_unit> q2back(q2si);

G输出:没有用于调用’conversion_factor(…,…)’的匹配函数.

这是否与dimensionless_type似乎是类型列表结尾的标记有关?

任何帮助或建议将不胜感激.谢谢.

解决方法:

Is this related to the fact that dimensionless_type seems to be a marker for end of typelists?

有点. dimensionless_type隐式包含在每个测量系统中,并以相同的方式从中提取,请参阅boost / units / dimensionless_units.hpp.

在“百分比”示例中,考虑一下您的新测量系统将是什么,以及如何根据通常的增压单位规则指定它:

namespace my {
    ... // define your own unit tag types
    typedef make_system</* your system's units with dimensions*/>::type system;
    ... // unit typedefs, consts, etc.
}

因此,如果你说你的百分比将是无量纲的,但与原始的无量纲不同,那么你就是在违背上述概念.因此,您无法在系统中定义2个无量纲维度.

Any help or suggestion would be greatly appreciated.

我在这里看到3个选项:

>覆盖基础数量类型并接受ctor中的百分比或常规数字.
http://www./doc/libs/release/doc/html/boost_units/Examples.html#boost_units.Examples.UDTExample
>如果你想以百分比显示事物,你可以尝试使用自动缩放功能(从未自己完成,但也有一个例子 – http://www./doc/libs/release/doc/html/boost_units/Examples.html#boost_units.Examples.autoscale).
>您可以创建特殊的自定义维度“百分比”,并显式转换为/来自百分比数量.这可能与您的原始意图最接近,但自动转换并不总是发生,因为库不是为“无量纲数量的维度分析”而设计的.如果您尝试强制系统进行自动转换,您可以看到结果有多丑:

//
// percentage (%)
//
namespace my {
    struct percent_base_dimension : 
        base_dimension<percent_base_dimension, 1> {};
    typedef percent_base_dimension::dimension_type percent_type;

    struct percent_base_unit :
        base_unit<percent_base_unit, percent_type, 1>
    {
        static std::string name() {return "percent";}
        static std::string symbol() {return "%";}
    };
    typedef make_system<percent_base_unit>::type system;
    typedef percent_base_unit::unit_type percent_unit;
    BOOST_UNITS_STATIC_CONSTANT(percent, percent_unit);
}

namespace boost { namespace units {

template<class T0, class T1>
struct conversion_helper<quantity<my::percent_unit, T0>, quantity<si::dimensionless, T1> >
{
    static quantity<si::dimensionless, T1> convert(const quantity<my::percent_unit, T0>& source)
    {
        return(quantity<si::dimensionless, T1>::from_value(1e-2 * source.value()));
    }
};

template<class T0, class T1>
struct conversion_helper<quantity<si::dimensionless, T0>, quantity<my::percent_unit, T1> >
{
    static quantity<my::percent_unit, T1> convert(const quantity<si::dimensionless, T0>& source)
    {
        return(quantity<my::percent_unit, T1>::from_value(1e 2 * source.value()));
    }
};

} }

int main()
{
    using namespace my;

    quantity<percent_unit> q2my(3*percent);

    //quantity<si::dimensionless> q2si(q2my);
    //The converter won't be picked up due to an explicit disable_if in quantity.hpp:
    // typename boost::disable_if<detail::is_dimensionless_system<System2> >::type* = 0
    //so we manually force the conversion here:
    auto conv = conversion_helper<quantity<percent_unit>, quantity<si::dimensionless> >::convert;
    quantity<si::dimensionless> q2si(conv(q2my));

    quantity<percent_unit> q2back(q2si);

    std::cout 
        << "q2my: " << q2my << std::endl
        << "q2si: " << q2si << std::endl
        << "q2back: " << q2back << std::endl
        ;
}

因此,手动执行此操作是个好主意

namespace my {
    template<class Y>
    quantity<si::dimensionless, Y> units(const quantity<percent_unit, Y>& source)
    {
        return(quantity<si::dimensionless, Y>::from_value(1e-2 * source.value()));
    }

    template<class Y>
    quantity<percent_unit, Y> percentage(const quantity<si::dimensionless, Y>& source)
    {
        return(quantity<percent_unit, Y>::from_value(1e 2 * source.value()));
    }

}

int main()
{
    using namespace my;

    quantity<percent_unit> q2my(3*percent);
    quantity<si::dimensionless> q2si(my::units(q2my));
    quantity<percent_unit> q2back(my::percentage(q2si));

    std::cout 
        << "q2my: " << q2my << std::endl
        << "q2si: " << q2si << std::endl
        << "q2back: " << q2back << std::endl
        ;
}

或者,更好的是,使用类型检查的好处(所以你只能在转换因子中犯一个错误):

template<class Y>
quantity<si::dimensionless, Y> units(const quantity<percent_unit, Y>& source)
{
    return quantity<si::dimensionless, Y>(1e-2 * source / percent);
}

template<class Y>
quantity<percent_unit, Y> percentage(const quantity<si::dimensionless, Y>& source)
{
    return quantity<percent_unit, Y>(1e 2 * source * percent);
}

来源:https://www./content-4-428651.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多