Boost logo

Boost :

From: Sylvain Pion (pion_at_[hidden])
Date: 2002-09-07 12:15:59


On Sat, Sep 07, 2002 at 11:13:20AM +0100, Paul A. Bristow wrote:
> The drawback of this is that in practice you only need sparse handful of
> intervals for significands numeric_limits<>::digits
> = 23, 24, 32, 53, 64, 80, 106, 112, 128 perhaps.
> So a switch, and perhaps some default, as Sylvain Pion suggested is quite
> adequate.

The template solution has advantages : it can simply refuse to compile if you
try to ask a digits/radix pair of values which you have not planned.
With the initial switch I proposed, you can only get a run-time error.

The template solution is also more easily extensible as Herve said : you don't
need to have only one big statement in only one file, you can spread the
various versions over several files.

So I guess you could write your constants like :

template <class T, int radix = std::numeric_limits<T>::radix,
                   int digits = std::numeric_limits<T>::digits>
class pi;

template <>
class pi<float, 2, 24>
{
  static const float upper = ...;
  static const float lower = ...;
  static const float nearest = ...;
};

Either you refuse to compile if the value is not planned, or you can have the
template falling back on a lower precision available :

template <class T, int radix, int digits>
class pi {
  static const T upper = pi<T, radix, digits-1>::upper;
  ...
};

Seems reasonnable ?

-- 
Sylvain

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