Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-01-27 01:06:45


I was just playing around, trying to compute a square root at compile
time. I came up with the following:

template < unsigned int N, unsigned int X = 1, unsigned int X2 = ( X + N
/ X ) / 2 >
struct Sqrt
{
    typedef typename Sqrt < N, X2 > :: ValueHolder ValueHolder;
};

template < unsigned int N, unsigned int X >
struct Sqrt < N, X, X >
{
    struct ValueHolder
    {
        enum ValueStorage
        {
            Value = X
        };
    };
};

This works for most values, but unfortunately some ( like 80 ) end up
oscillating and X never becomes equal to X2. How could I go about
correcting this?

-Jason


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