Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 2001-10-24 23:37:08


Here is yet another suggestion. It allows most users to just write
    v = pi
and get a pretty good approximation to pi, and pronto.

And it allows generic programmers to write things like:

    template<typename T> mashed_pi() {
        return mash(constant<T>(pi));
    }

And it allows boost::math::constants library implementors to provide
specializations that are faster, more precise, or whatever.

And it provides a pattern for other numeric libraries to take advantage
of, e.g.:

      template<> inline rational<int> constant(const pi_&) {
         return rational<int>(5419351,1725033);
      }

Here is a sketch of an implementation:

   namespace constants {

      // an example constant, suitable for everyday abuse
      static const struct pi_ {
         pi_(){}
         inline operator long double() const {
            return 3.141592653589793238462643383279502884197L;
         }
      } pi;

      // a generic constant generator
      template<typename T, typename V> inline T constant(const V& v) {
         return static_cast<T>(v);
      }

      // example specializations of the constant generator
      template<> inline float constant(const pi_&) {
         return 3.141593F;
      }
      template<> inline double constant(const pi_&) {
         return 3.14159265358979;
      }
      template<> inline long double constant(const pi_&) {
         return 3.141592653589793238L;
      }
   }


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