Boost logo

Boost :

From: Ed Brey (brey_at_[hidden])
Date: 2001-02-09 10:00:14


> As a result I am starting again with a much simpler proposal -
> a file of long double #defined constants
>
> #define SQRT2 1.41421356237309504880168872420969807857L
> // and lots more
>
> and some recommendations for C++ use:
>
> const float sqrt_2 = static_cast<float>SQRT2;

The macro seems like a good, pragmatic way of minimizing loss of precision
within a compiler. To take this a step further, perhaps there is a way to
eliminate the macros altogether with the help of templates. Consider this
having the generator output a header like this:

namespace boost { namespace constant {
template<typename T> inline T sqrt_2() {
 return static_cast<T>(1.41421356237309504880168872420969807857L);
}
}}

The user code would then look more like this:

int main() {
 using boost::constant;
 std::cout << constant::sqrt_2<float>() << '\n';
}

Of course, assigning a local variable to take on the constant value is
fine, too. The templatized format reminds me of numeric_limits, only with
a templatized function instead of a class. A class could be used as well,
allowing for usage like constant::sqrt_2<float>::value, but I can't think
of where there would be an advantage here. For integers, having a
constant instead of a function is useful, but for floating types I don't
know of such an advantage.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk