Boost C++ Libraries: Ticket #10270: square root of boost units scaled_unit fails to compile https://svn.boost.org/trac10/ticket/10270 <p> I have created a scaled unit to handle micro meters: </p> <pre class="wiki">typedef make_scaled_unit&lt;si::length, scale&lt;10, static_rational&lt;-6&gt; &gt; &gt;::type micro_meter_unit; </pre><p> Everything works as expected: </p> <pre class="wiki">quantity&lt;si::length, double&gt; some_meter = 10 * si::meter; quantity&lt;micro_meter_unit, double&gt; some_mu_meter = static_cast&lt;quantity&lt;micro_meter_unit, double&gt;&gt;(some_meter); std::cout &lt;&lt; "some_meter^2 = " &lt;&lt; some_meter * some_meter; // outputs 100 m^2 std::cout &lt;&lt; "some_mu_meter^2 = " &lt;&lt; some_mu_meter * some_mu_meter; // outputs 1e+014 p(m^2) </pre><p> Yet the square root operations is not compiled: </p> <pre class="wiki">std::cout &lt;&lt; "sqrt(some_meter) = " &lt;&lt; sqrt(some_meter); // outputs 3.16228 m^(1/2) std::cout &lt;&lt; "sqrt(some_mu_meter) = " &lt;&lt; sqrt(some_mu_meter); //error here // 'value' : is not a member of 'boost::units::scale_dim_tag' </pre><p> Also the following code fails to compile: </p> <pre class="wiki">quantity&lt;si::length, double&gt; mu_meter(sqrt(1.0 * si::micro * si::meter * si::meter)); </pre><p> I used the following include files: </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;boost/units/quantity.hpp&gt; #include &lt;boost/units/make_scaled_unit.hpp&gt; #include &lt;boost/units/systems/si.hpp&gt; #include &lt;boost/units/cmath.hpp&gt; #include &lt;boost/units/io.hpp&gt; #include &lt;boost/units/scale.hpp&gt; #include &lt;boost/units/systems/si/prefixes.hpp&gt; </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10270 Trac 1.4.3 bnorman@… Tue, 29 Jul 2014 21:02:27 GMT <link>https://svn.boost.org/trac10/ticket/10270#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10270#comment:1</guid> <description> <p> I'm hung up on exactly the same problem. </p> </description> <category>Ticket</category> </item> <item> <author>bnorman@…</author> <pubDate>Tue, 29 Jul 2014 22:26:03 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10270#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10270#comment:2</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/10270#comment:1" title="Comment 1">bnorman@…</a>: </p> <blockquote class="citation"> <p> I'm hung up on exactly the same problem. </p> </blockquote> <p> FYI, I am now able to work around this issue as follows: </p> <pre class="wiki">typedef scaled_base_unit&lt;si::meter_base_unit, scale&lt;10, static_rational&lt;-6&gt; &gt; &gt;::type micro_meter_base_unit; typedef micro_meter_base_unit::unit_type micro_meter_unit; </pre> </description> <category>Ticket</category> </item> <item> <author>Wouter Boomsma <wb@…></author> <pubDate>Sat, 31 Jan 2015 00:53:20 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10270#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10270#comment:3</guid> <description> <p> I'm stuck with the same problem. Is there any hope for a fix in the near future? It would really be greatly appreciated. </p> <p> As far as I can see, the suggested work-around does not work in my case, since the scaled unit is not a base unit. In my application, I need a kilojoule unit. The obvious approach would be: </p> <blockquote> <p> typedef make_scaled_unit&lt;si::energy, scale&lt;10, static_rational&lt;3&gt; &gt; &gt;::type energy_unit_kJ; </p> </blockquote> <p> However, if this unit is involved in any nontrivial calculations involving sqrt or pow, it fails as described above. </p> <p> The scaled_base_unit workaround suggested by bnorman does not work directly since joule is not a base unit. Instead, I attempted to define a base unit from scratch, and set up the conversion factory manually: </p> <p> struct energy_base_unit_kJ: base_unit&lt;energy_base_unit_kJ, energy_dimension, 1&gt; { </p> <blockquote> <p> static std::string name() {return "kilojoule";} </p> </blockquote> <blockquote> <p> static std::string symbol() { return "kJ"; } </p> </blockquote> <p> }; </p> <p> BOOST_UNITS_DEFINE_CONVERSION_FACTOR( </p> <blockquote> <p> energy_base_unit_kJ, si::energy, double, 1/1000.0); </p> </blockquote> <p> The problem is that I cannot define the reverse conversion factor because si::energy is not a base unit, and BOOST_UNITS_DEFINE_CONVERSION_FACTOR requires its first argument to be a base unit. </p> <p> If there is some clever way to work around this, I would appreciate a pointer in the right direction - but under all circumstances, it would really be convenient if make_scaled_unit just worked. </p> </description> <category>Ticket</category> </item> <item> <author>petamas@…</author> <pubDate>Fri, 20 Mar 2015 12:14:54 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10270#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10270#comment:4</guid> <description> <p> Hi all, here's a fix for this problem: </p> <pre class="wiki">namespace boost { namespace mpl { template&lt;&gt; struct divides_impl&lt;boost::units::scale_dim_tag,boost::units::detail::static_rational_tag&gt; { template&lt;class T0, class T1&gt; struct apply { typedef boost::units::scale_list_dim&lt; boost::units::scale&lt; (T0::base), typename mpl::divides&lt;typename T0::exponent, T1&gt;::type &gt; &gt; type; }; }; } } </pre><p> The main problem is that boost::mpl::divides does not work for a scale_list_dim and a static_rational. Power works because boost::mpl::times is implemented in boost/units/detail/unscale.hpp. (In fact, my fix is the copy of that code, replacing "times" with "divides".) </p> <p> I'll try to add the fix to the library, but it's my first time on boost trac, so I don't know how long that'll take. </p> </description> <category>Ticket</category> </item> <item> <author>Wouter Boomsma <wb@…></author> <pubDate>Tue, 24 Mar 2015 21:41:13 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10270#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10270#comment:5</guid> <description> <p> Thanks! It would be greatly appreciated if you could submit this fix to Boost trunk. </p> </description> <category>Ticket</category> </item> <item> <author>Tamas Peregi <petamas@…></author> <pubDate>Tue, 24 Mar 2015 22:20:01 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10270#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10270#comment:6</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/10270#comment:5" title="Comment 5">Wouter Boomsma &lt;wb@…&gt;</a>: </p> <blockquote class="citation"> <p> Thanks! It would be greatly appreciated if you could submit this fix to Boost trunk. </p> </blockquote> <p> I'll try, I hope I'll have time to look into submitting on the weekend. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 19 Feb 2016 17:43:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10270#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10270#comment:7</guid> <description> <p> The bug persists </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 25 May 2017 11:18:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10270#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10270#comment:8</guid> <description> <p> The bug persists. Patch by "petamas" fixes compilation problem. Thanks a lot! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Jürgen Hunold</dc:creator> <pubDate>Tue, 02 Jan 2018 16:32:25 GMT</pubDate> <title>status, version changed; resolution set https://svn.boost.org/trac10/ticket/10270#comment:9 https://svn.boost.org/trac10/ticket/10270#comment:9 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>version</strong> <span class="trac-field-old">Boost 1.55.0</span> → <span class="trac-field-new">Boost Development Trunk</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Final pull request <a class="ext-link" href="https://github.com/boostorg/units/pull/27"><span class="icon">​</span>https://github.com/boostorg/units/pull/27</a> merged in <a class="ext-link" href="https://github.com/boostorg/units/commit/3c5d74a1e1ac244b31370f1baf74170942af1f2e"><span class="icon">​</span>https://github.com/boostorg/units/commit/3c5d74a1e1ac244b31370f1baf74170942af1f2e</a> </p> Ticket