簡體   English   中英

如何找到帶有 scaled_unit 和帶有 boost::units 的 si::unit 的卷?

[英]How do I find a volume with scaled_unit and si::unit with boost::units?

我需要通過面積乘以 mm^3 的高度來找到體積。 區域的問題,它必須以 mm^2 為單位,但 boost 沒有這樣的內置選項,所以我需要制作這個單元(我想用make_scaled_unit )。 我嘗試按照此答案boost 文檔中所示的方式進行操作,但在 boost 庫的 quantity.hpp 中出現錯誤:

C2338 (is_implicitly_convertible<Unit2,unit_type>::value == true)。

和一條消息, see reference to function template instantiation的參考,其中包含我完全不理解的巨大類型名稱。 我該如何正確解決這個問題?

這是我嘗試使其工作的代碼

    #include <iostream>
    #include <boost/units/systems/si/volume.hpp>
    #include <boost/units/systems/si/mass.hpp>
    #include "string"
    #include <boost/units/systems/si/io.hpp>
    #include <boost/units/systems/si/prefixes.hpp>
    #include <boost/units/systems/angle/degrees.hpp>
    #include <cmath>
    
    using namespace boost::units;
    using namespace boost::units::si;
    using Volume = boost::units::quantity<boost::units::si::volume>;
    using Square = boost::units::quantity<boost::units::area_dimension>;

    // like in stackoverflow answer
    namespace extended_area_names {
    namespace squared_millimeters_system
    {
        // "area" is not a base unit
        typedef make_scaled_unit<area, scale<10, static_rational<-6>>>::type millimeter_unit;
        typedef make_system<millimeter_unit>::type system;
        typedef unit<area_dimension, system> area;

        BOOST_UNITS_STATIC_CONSTANT(squared_millimeter, area);
        BOOST_UNITS_STATIC_CONSTANT(squared_millimeters, area);
    } // namespace squared_millimeters_system

    typedef quantity<squared_millimeters_system::area> quantity_area_square_millimeter;
    using squared_millimeters_system::squared_millimeter;
    using squared_millimeters_system::squared_millimeters;
    } // namespace extended_area_names

        // like in boost documentation, I use only one of these per try
    typedef make_scaled_unit<area, scale<10, static_rational<-6>>>::type millimeter_unit;

    Volume ContentVolume(std::string tankType, double contentLevel_raw, double tankHeight_raw, double tankDiameter_raw)
    {
    quantity<length> contentLevel(contentLevel_raw * milli * meters);
    quantity<length> tankHeight(tankHeight_raw * milli * meters);
    quantity<length> tankDiameter(tankDiameter_raw * milli * meters);
    quantity<length> tankRadius = tankDiameter / 2.0;
    Volume tankVolume(3.1415 * (tankRadius * tankRadius) * tankHeight);
    Volume contentVolume;


    using namespace extended_area_names;

    quantity
        <unit<plane_angle_dimension, degree::system>
        > sectorAngle(2 * acos((tankRadius - tankHeight) / tankRadius)
            * degree::degree);

    quantity<millimeter_unit> sectorSquare(
        (sectorAngle.value() * (tankRadius.value() * tankRadius.value()) / 2.0)
        * square_meter);

    quantity<length> triangleBase(
        sqrt(
            (tankRadius.value() * tankRadius.value()) -
            ((tankRadius.value() - tankHeight.value()) *
                (tankRadius.value() - tankHeight.value())))
        * milli * meters);

    quantity<length> p(
        (tankRadius + tankRadius + triangleBase) / 2.0);

    quantity<millimeter_unit> triangleSquare(
        sqrt(
            p.value() *
            (p.value() - tankRadius.value()) *
            (p.value() - tankRadius.value()) *
            (p.value() - triangleBase.value()))
        * square_meter);

    // the message says the problem is here
    contentVolume = (sectorSquare - triangleSquare) * tankHeight;

    return contentVolume; //m^3
    }
    }
    }

boost沒有這樣的內置選項

我發現,實際上是有的。 您只需要使用<boost/units/systems/si/prefixes.hpp>庫,然后像這樣構建

quantity<area> example(2.0 * milli * milli * square_meters)

會工作得很好。 但我必須說,在任何 output 上,它都會打印類似2e-06 m^2的內容,所以如果你還需要更改 output 這種方法對你不起作用。 希望這會幫助任何遇到同樣問題的人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM