[Boost-bugs] [Boost C++ Libraries] #12261: LT_HalfPrevLoT and GT_HalfSuccHiT are not symmetrical

Subject: [Boost-bugs] [Boost C++ Libraries] #12261: LT_HalfPrevLoT and GT_HalfSuccHiT are not symmetrical
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-06-10 12:38:46


#12261: LT_HalfPrevLoT and GT_HalfSuccHiT are not symmetrical
-------------------------------------------------+-------------------------
 Reporter: Christophe Brassart | Owner: viboes
  <christophe.brassart@…> | Status: new
     Type: Bugs | Component: convert
Milestone: To Be Determined | Severity: Problem
  Version: Boost 1.61.0 |
 Keywords: |
-------------------------------------------------+-------------------------
 Reusing low boost conversion policies to implement my own roundToInt
 method...

 If you try the following code:

 {{{
 template <class To, class From>
 struct NumericRangeCheckerTraits {
         typedef To target_type;
         typedef From source_type;
         typedef From argument_type;
 };

 struct ToTextOverflowHandlerPolicy {
         void operator() ( boost::numeric::range_check_result result) {
                 switch (result) {
                 case boost::numeric::cInRange:
                         std::cout << "the value is in range"<< std::endl;
                         break;
                 case boost::numeric::cNegOverflow:
                 case boost::numeric::cPosOverflow:
                         std::cout << "the value is out of range"<<
 std::endl;
                         break;
                 }
         };
 };

 double dmax = std::numeric_limits<int>::max() + 0.5;
 double dmin = std::numeric_limits<int>::min() - 0.5;

 boost::numeric::convdetail::generic_range_checker<NumericRangeCheckerTraits<int,
 double>,
 boost::numeric::convdetail::LT_HalfPrevLoT<NumericRangeCheckerTraits<int,
 double>>,
 boost::numeric::convdetail::GT_HalfSuccHiT<NumericRangeCheckerTraits<int,
 double>>,
         ToTextOverflowHandlerPolicy>::validate_range(dmax);

 boost::numeric::convdetail::generic_range_checker<NumericRangeCheckerTraits<int,
 double>,
 boost::numeric::convdetail::LT_HalfPrevLoT<NumericRangeCheckerTraits<int,
 double>>,
 boost::numeric::convdetail::GT_HalfSuccHiT<NumericRangeCheckerTraits<int,
 double>>,
         ToTextOverflowHandlerPolicy>::validate_range(dmin);
 }}}


 You will get:


 {{{
 the value is out of range
 the value is in range
 }}}


 When they should be both out of range.

 This is because GT_HalfSuccHiT should implement range_check_result as
 such:


 {{{
 static range_check_result apply ( argument_type s ) {
   return s > static_cast<S>(bounds<T>::highest()) + static_cast<S>(0.5)
            ? cPosOverflow : cInRange ;
 }}}


 instead of using >=.

 The comment to describe GT_HalfSuccHiT should be changed as well to:

 {{{
 // s > Highest(T) + 0.5 ? cPosgOverflow : cInRange
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12261>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:20 UTC