Boost C++ Libraries: Ticket #13591: maybe a bug in math::constants library https://svn.boost.org/trac10/ticket/13591 <p> The following templated code will generate incorrect result in visual studio community 2017: </p> <pre class="wiki">#include&lt;iostream&gt; #include&lt;boost/math/constants/constants.hpp&gt; // convienent defination of math constants template&lt;typename T&gt; const T pi = boost::math::constants::pi&lt;T&gt;(); // PI template&lt;typename T&gt; const T two_pi = boost::math::constants::two_pi&lt;T&gt;(); // 2*PI // physical constants template&lt;typename T&gt; const T mu_0 = 4.0*pi&lt;T&gt;*1.0e-4; // permeability of free space in H / km int main() { using T = double; auto w = two_pi&lt;T&gt; * 0.01; //auto t1 = pi&lt;T&gt;; auto t2 = mu_0&lt;T&gt;; std::cout&lt;&lt;"omega="&lt;&lt;w&lt;&lt;std::endl; std::cout&lt;&lt;"mu_0="&lt;&lt;t2&lt;&lt;std::endl; system("pause"); return 0; } </pre><p> In VS2017, mu_0 will be 0.000. However, if I uncomment </p> <pre class="wiki">auto t1 = pi&lt;T&gt; </pre><p> in main, the result is correct. This problem does not happen when compiled with gcc 8.1 in Manjaro Linux. The boost library version is 1.67.0 </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13591 Trac 1.4.3 John Maddock Tue, 31 Jul 2018 18:03:03 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/13591#comment:1 https://svn.boost.org/trac10/ticket/13591#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">obsolete</span> </li> </ul> <p> Moved to <a class="ext-link" href="https://github.com/boostorg/math/issues/144"><span class="icon">​</span>https://github.com/boostorg/math/issues/144</a> </p> Ticket John Maddock Sat, 18 Aug 2018 18:28:45 GMT <link>https://svn.boost.org/trac10/ticket/13591#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13591#comment:2</guid> <description> <p> Not sure, but I think this is an order of initialization error, with mu_0 being initialized before pi. </p> <p> Whatever I can reduce the issue down to: </p> <pre class="wiki">template &lt;class T&gt; T get_pi() { return 3.14; } template &lt;class T&gt; T get_2_pi() { return 6.28; } template&lt;typename T&gt; const T pi = get_pi&lt;T&gt;(); // PI template&lt;typename T&gt; const T two_pi = get_2_pi&lt;T&gt;(); // 2*PI // physical constants template&lt;typename T&gt; const T mu_0 = 4.0*pi&lt;T&gt;*1.0e-4; int main() { using T = double; auto w = two_pi&lt;T&gt; * 0.01; //auto t1 = pi&lt;T&gt;; // uncomment this line and everything works OK. auto t2 = mu_0&lt;T&gt;; std::cout &lt;&lt; "omega=" &lt;&lt; w &lt;&lt; std::endl; std::cout &lt;&lt; "mu_0=" &lt;&lt; t2 &lt;&lt; std::endl; system("pause"); return 0; } </pre><p> So it's not our bug if it's a bug at all? </p> <p> Note: please reply on the linked github issue as this Trac is now closed. </p> </description> <category>Ticket</category> </item> </channel> </rss>