
Boost : 
From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 20011025 08:15:16
 Original Message 
From: Greg Colvin <gcolvin_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, October 25, 2001 1:37 AM
Subject: Re: [boost] Math constants for naive and gurus?  which constants
do you want?
> 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;
> }
> }
>
>
>
This looks really good to me!
I can't see any drawback... I think I'll buy it.
Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk