Ticket #6047: ticket6047_round_trunc_01242013.diff

File ticket6047_round_trunc_01242013.diff, 2.3 KB (added by Evelyn Wu <genevivewoo@…>, 10 years ago)
  • trunc.hpp

     
    1616
    1717namespace boost{ namespace math{
    1818
     19template < class T, class TargetType,  class Policy >
     20inline TargetType trunc_cast(T const& x, Policy const& pol)
     21{
     22BOOST_STATIC_ASSERT(std::numeric_limits<TargetType>::is_specialized); 
     23T r = trunc(x);
     24if ((r> (std::numeric_limits<TargetType>::max)()) ||
     25    (r < (std::numeric_limits<TargetType>::min)()))
     26  return policies::raise_rounding_error
     27  ("boost::math::round_cast<%1%,TargetType>(%%1%%)>",
     28   "Value %1% exceeds the numeric limits",
     29   r,
     30   0,
     31   pol);
     32  return static_cast<TargetType> (r);
     33 
     34}
     35 
     36template <class T, class TargetType>
     37inline TargetType trunc_cast(T const& x) 
     38{
     39  typedef typename policies::policy<
     40  boost::math::policies::rounding_error<policies::throw_on_error>
     41  > Policy;
     42  return trunc_cast<T, TargetType, Policy>(x, Policy());
     43}
     44
     45 
    1946template <class T, class Policy>
    2047inline T trunc(const T& v, const Policy& pol)
    2148{
  • round.hpp

     
    1414#include <boost/math/policies/error_handling.hpp>
    1515#include <boost/math/special_functions/fpclassify.hpp>
    1616
     17
    1718namespace boost{ namespace math{
    1819
     20
     21template < class T, class TargetType,  class Policy >
     22inline TargetType round_cast(T const& x, Policy const& pol)
     23{
     24  BOOST_STATIC_ASSERT(std::numeric_limits<TargetType>::is_specialized); 
     25  T r = round(x);
     26  if ((r> (std::numeric_limits<TargetType>::max)()) ||
     27      (r < (std::numeric_limits<TargetType>::min)()))
     28    return policies::raise_rounding_error
     29      ("boost::math::round_cast<%1%,TargetType>(%%1%%)>",
     30        "Value %1% exceeds the numeric limits",
     31        r,
     32        0,
     33        pol);
     34  return static_cast<TargetType> (r);
     35
     36}
     37
     38template <class T, class TargetType>
     39inline TargetType round_cast(T const& x) 
     40{
     41  typedef typename policies::policy<
     42  boost::math::policies::rounding_error<policies::throw_on_error>
     43  > Policy;
     44  return round_cast<T, TargetType, Policy>(x, Policy());
     45}
     46 
    1947template <class T, class Policy>
    2048inline T round(const T& v, const Policy& pol)
    2149{