I would do
template<typename T>
T const pi = std::acos(-T(1));
or
template<typename T>
T const pi = std::arg(-std::log(T(2)));
I would not typing in π to the precision you need. What is that even supposed to mean? The precision you need is the precision of T
, but we know nothing about T
.
You might say: What are you talking about? T
will be float
, double
or long double
. So, just type in the precision of long double
, i.e.
template<typename T>
T const pi = static_cast<T>(/* long double precision π */);
But do you really know that there won't be a new floating point type in the standard in the future with an even higher precision than long double
? You don't.
And that's why the first solution is beautiful. You can be quite sure that the standard would overload the trigonometric functions for a new type.
And please, don't say that the evaluation of a trigonometric function at initialization is a performance penalty.
3.14
,3.141592
andatan(1) * 4
? – Nikola Malešević Sep 6 '12 at 16:09