Boost logo

Boost :

From: Gennadiy Rozental (gennadiy.rozental_at_[hidden])
Date: 2006-08-07 12:19:14


"John Maddock" <john_at_[hidden]> wrote in message
news:00f101c6b86f$68577300$96f20352_at_fuji...
> Gennadiy Rozental wrote:
>> Could you try this?
>
> I sympathise, but there is a problem still: there are some std lib's
> (STLport springs to mind, plus some Borland versions) where
> numeric_limits<>::is_specialized is *not* a compile time constant. It
> doesn't effect all compilers/std lib's, and we have a config macro
> BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS for this, but then you're into
> applying workarounds on the workaround :-(

Ok how about this:

namespace tt_detail {

template<typename FPT>
struct fpt_limits {
    FPT min_value()
    {
        return std::numeric_limits<FPT>::is_specialized
                    ? (std::numeric_limits<FPT>::min)()
                    : 0;
    }
    FPT max_value()
    {
        return std::numeric_limits<FPT>::is_specialized
                    ? (std::numeric_limits<FPT>::max)()
                    : static_cast<FPT>(1000000);
    }
};

template<typename FPT>
inline FPT
safe_fpt_division( FPT f1, FPT f2 )
{
    // Avoid overflow.
    if( f2 < static_cast<FPT>(1) && f1 > f2*fpt_limits<FPT>::max_value() )
        return fpt_limits<FPT>::max_value();

    // Avoid underflow.
    if( f1 == static_cast<FPT>(0) ||
        f2 > static_cast<FPT>(1) && f1 < f2*fpt_limits<FPT>::min_value() )
        return static_cast<FPT>(0);

    return f1/f2;
}

} // namespace tt_detail

Gennadiy


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