Boost logo

Boost :

From: Sylvain Pion (pion_at_[hidden])
Date: 2002-09-06 17:56:13


On Fri, Sep 06, 2002 at 06:11:25PM -0400, Herve Bronnimann wrote:
> Just occured to me: If you want to take advantage of compile-time,
> template arguments of the form <unsigned int> would work, and
> specializations for various ints would give you the desired rounding.
>
> Example (the constants are bogus, but you'll get my point):
>
> template <unsigned int decimal_precision>
> struct interval_pi {
> static const double lower = 3; // default version
> static const double upper = 4;
> };
>
> #define BOOST_INTERVAL_SPECIALIZE_PI_CONSTANT(n, l, u) \
> template <> struct interval_pi<n> { \
> static const double lower = l; \
> static const double upper = u; \
> }
>
> BOOST_INTERVAL_SPECIALIZE_PI_CONSTANT(1, 3.1 , 3.2 );
> BOOST_INTERVAL_SPECIALIZE_PI_CONSTANT(2, 3.14 , 3.15 );
> BOOST_INTERVAL_SPECIALIZE_PI_CONSTANT(3, 3.141 , 3.142 );
> BOOST_INTERVAL_SPECIALIZE_PI_CONSTANT(4, 3.1415 , 3.1416 );
> BOOST_INTERVAL_SPECIALIZE_PI_CONSTANT(5, 3.14159 , 3.14160 );
> BOOST_INTERVAL_SPECIALIZE_PI_CONSTANT(6, 3.1415926 , 3.1415927 );
> BOOST_INTERVAL_SPECIALIZE_PI_CONSTANT(7, 3.14159265 , 3.14159266 );
> BOOST_INTERVAL_SPECIALIZE_PI_CONSTANT(8, 3.141592653 , 3.141592654 );
> BOOST_INTERVAL_SPECIALIZE_PI_CONSTANT(9, 3.1415926535 , 3.1415926536 );
>
> std::cout << interval_pi<7>::lower << std::endl;
>
> You can go on to provide all the (precomputed) precisions ever needed by
> users. It's a little less flexible than switches ( ? : ) but it's
> perhaps more readable... ?

Doesn't work.

You miss the point of where the problem is, and Gaby as well it seems.

My problem is not to generate compile-time constants alone, nor to generate
correct constants alone. I want _both_, and _this_ is harder.

The fact is that the standard (always this good ol' 2.13.3.1) says that, since
the (decimal) numbers that you use are not representable (*), then the compiler
will have the choice between the two representable numbers enclosing it, and
can pick either one. Hence it can pick the one I don't want. So it doesn't
work.

Go read again my proposed solution (going through representable numbers) once
you've understood the problem. And then try to finally propose me the working
nicer solution of my dreams :)

-- 
Sylvain
(*) 3.1 is NOT representable in binary, whatever value "digits" has, since it
    has infinite representation in base 2.
    Same for most, if not all the ones you use above.

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