Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#13591 closed Bugs (obsolete)

maybe a bug in math::constants library

Reported by: seanxnie@… Owned by: John Maddock
Milestone: Boost 1.67.0 Component: math
Version: Boost 1.67.0 Severity: Problem
Keywords: Cc:

Description

The following templated code will generate incorrect result in visual studio community 2017:

#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;
}

In VS2017, mu_0 will be 0.000. However, if I uncomment

auto t1 = pi<T>

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

Change History (2)

comment:1 by John Maddock, 4 years ago

Resolution: obsolete
Status: newclosed

comment:2 by John Maddock, 4 years ago

Not sure, but I think this is an order of initialization error, with mu_0 being initialized before pi.

Whatever I can reduce the issue down to:

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;
}

So it's not our bug if it's a bug at all?

Note: please reply on the linked github issue as this Trac is now closed.

Note: See TracTickets for help on using tickets.