This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

math constants


Working on some code which we'll hopefully send out soon it happens
often that in template code variables have to be initialized with the
constants available in the C math.h header: M_PI etc.  The constants
are double, the glibc header defines non-standard M_*l constants.
Therefore code looks like this:

#ifdef M_PIl
  const _RealType pi = std::is_same<_RealType, long double>::value ?
M_PIl, _RealType(M_PI));
#else
  const _RealType pi = _RealType(M_PI);
#endif

Aside from the fact that this is unnecessarily long it also means some
platforms won't have all the precision.  Shouldn't we add as an
extension some constants like this:

template<typename _RealType>
struct math_constants {
  static constexpr _RealType pi =
_RealType(3.141592653589793238462643383279502884197169399375105820974944L);
  static constexpr _RealType root_pi =
_RealType(1.772453850905516027298167483341145182797549456122387128213808L);
};

This variant would be implemented without any macros.  With macros the
ugly long double constant in _RealType constructor could be avoided
but at least -Wall doesn't warn.

Would this be an acceptable patch?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]