[numeric::converter] finding the smallest c such that (a/c) + (b/c) < inf

Hi All, template<typename T> struct promote{}; template<> struct promote<float> : mpl::identity<double>{}; template<> struct promote<double> : mpl::identity<long double>{}; template<typename T> //T: float, double struct fit_a_plus_b_to_range{ static T divider(T a, T b){ static const T mx = math::tools::max_value<T>(); static const T mi = math::tools::min_value<T>(); if(a > (mx - b)){ typedef typename promote<T>::type prom_t; typedef numeric::converter<T,prom_t> up_t; typedef numeric::converter<prom_t,T> down_t; prom_t au = up_t::convert(a); prom_t bu = up_t::convert(b); prom_t mu = up_t::convert(mx); prom_t cu = ((au/mu)+(bu/mu)); T delta = ...; T c = down_t::convert(cu) + delta; BOOST_ASSERT(!math::isinf((a/c)+(b/c))); return c; }else{ return (T)(1); } } }; With delta = 0.0 the assertion fails, with delta = 1.0 it works (for a few examples). What's the smallest delta value? Is there a better way to do this overall? If this is off topic, let me know...

AMDG er wrote:
With delta = 0.0 the assertion fails, with delta = 1.0 it works (for a few examples). What's the smallest delta value? Is there a better way to do this overall?
This seems to work and the loops shouldn't be executed very many times. template<class T> T fit_to_range(const T& x, const T& y) { double max = (std::numeric_limits<T>::max)(); if(x > max - y) { double c = x/max + y/max; while(!boost::math::isinf(x/c + y/c)) { c = boost::math::float_prior(c); } while(boost::math::isinf(x/c + y/c)) { c = boost::math::float_next(c); } return(c); } else { return(static_cast<T>(1)); } } In Christ, Steven Watanabe

AMDG er wrote:
Steven Watanabe wrote:
template<class T> T fit_to_range(const T& x, const T& y) { double max = (std::numeric_limits<T>::max)();
Very helpful, thanks. May I ask : why double, not T?
cut and paste error. I was originally testing with plain doubles. In Christ, Steven Watanabe
participants (2)
-
er
-
Steven Watanabe