Boost logo

Boost Users :

Subject: Re: [Boost-users] [math] iround / llround with rounding error
From: John Maddock (boost.regex_at_[hidden])
Date: 2010-12-13 07:22:35


> we want to use iround / llround without exception handling. This is
> possible
> with the 'user_error' option and override a 'user_rounding_error'
> function.
>
> We want to return numeric_limits<int>::max() or
> numeric_limits<__int64>::max()
> if the rounding overflows over the maximum integer domain. This is then
> problematic since the 'user_rounding_error' has a template argument only
> for
> its source type (e.g. float / double), but not for its target type (i.e.
> int /
> __int64).
>
> We can ofc do the check before calling iround / llround, but this check
> would
> then executed be twice for the normal non overflow case.

This looks like a bug in our design frankly, I'll try and figure out how to
fix this, in the mean time, you could simply implement your own versions of
these since they're basically trivial wrappers around "round" which does all
the work:

template <class T, class Policy>
inline int iround(const T& v, const Policy& pol)
{
   BOOST_MATH_STD_USING
   T r = boost::math::round(v, pol);
   if(fabs(r) > (std::numeric_limits<int>::max)())
      return
static_cast<int>(policies::raise_rounding_error("boost::math::iround<%1%>(%1%)",
0, v, pol)); // change this line
   return static_cast<int>(r);
}

HTH, John.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net