Boost logo

Boost :

From: Ed Brey (brey_at_[hidden])
Date: 2001-04-25 08:29:26


From: "Paul A. Bristow" <pbristow_at_[hidden]>

> Please can someone (Jes?) produce a detailed prototype of the format
> I should produce them in (like the one I produced but just for pi say.
> (and an example of how to use them too please).

The following prototype is similar to one that I produced a while back,
which I don't recall hearing any complaint over. I'm sure someone will
let me know if this leaves any requirements unfulfilled:

namespace boost { namespace math {
  template<typename T> struct constants {};
  template<> struct constants<float> {
    static float pi() {return 3.14F;}
  };
  template<> struct constants<double> {
    static double pi() {return 3.14;}
  };
  template<> struct constants<long double> {
    static long double pi() {return 3.14L;}
  };

  template<typename T> struct tailored_constants: constants<T> {};
  #ifdef PLATFORM1
    template<> struct tailored_constants<float> {
      static float pi() {union {unsigned u; float f;} b = {0x12345678};
return b.f;}
    };
    template<> struct tailored_constants<double> {
      static double pi() {return ...;}
    };
    template<> struct tailored_constants<long double> {
      static long double pi() {return ...;}
    };
  #endif
}}

Usage would typically be:
typedef boost::math::constants<float> c;
a = c::pi() * r * r;
hypotenuse = isosceles_triangle_leg * c::sqrt2();
  // sqrt2 is a placeholder for the proper name according to the naming
convention.

I wouldn't worry about the binary values for specific platforms at this
time, meaning ignore the #ifdef section.
The actual file that follows this prototype (as with most prototypes),
can be automatically generated, so there need be no concern over
repetitive strain. Moreover, generation should be preferred over hand
crafting wherever possible for this domain to help avoid human mistakes.


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