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<iostream>
#include<boost/math/constants/constants.hpp>
// convienent defination of math constants
template<typename T> const T pi = boost::math::constants::pi<T>(); // PI
template<typename T> const T two_pi = boost::math::constants::two_pi<T>(); // 2*PI
// physical constants
template<typename T> const T mu_0 = 4.0*pi<T>*1.0e-4; // permeability of free space in H / km
int main()
{
using T = double;
auto w = two_pi<T> * 0.01;
//auto t1 = pi<T>;
auto t2 = mu_0<T>;
std::cout<<"omega="<<w<<std::endl;
std::cout<<"mu_0="<<t2<<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<T>
</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-usBoost C++ Libraries/htdocs/site/boost.png
https://svn.boost.org/trac10/ticket/13591
Trac 1.4.3John MaddockTue, 31 Jul 2018 18:03:03 GMTstatus 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>
TicketJohn MaddockSat, 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 <class T>
T get_pi()
{
return 3.14;
}
template <class T>
T get_2_pi()
{
return 6.28;
}
template<typename T> const T pi = get_pi<T>(); // PI
template<typename T> const T two_pi = get_2_pi<T>(); // 2*PI
// physical constants
template<typename T> const T mu_0 = 4.0*pi<T>*1.0e-4;
int main()
{
using T = double;
auto w = two_pi<T> * 0.01;
//auto t1 = pi<T>; // uncomment this line and everything works OK.
auto t2 = mu_0<T>;
std::cout << "omega=" << w << std::endl;
std::cout << "mu_0=" << t2 << 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>