Re: [Boost-bugs] [Boost C++ Libraries] #11437: rolling_mean returns incorrect result when using unsigned int

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #11437: rolling_mean returns incorrect result when using unsigned int
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-01-13 18:50:23


#11437: rolling_mean returns incorrect result when using unsigned int
--------------------------------------+--------------------------
  Reporter: Gareth White <gwhite@…> | Owner: eric_niebler
      Type: Bugs | Status: new
 Milestone: To Be Determined | Component: accumulator
   Version: Boost 1.58.0 | Severity: Regression
Resolution: | Keywords:
--------------------------------------+--------------------------

Comment (by Martin):

 Another workaround, which allows sticking with unsigned types, is to
 declare the accumulator_set using lazy_rolling_mean:

 {{{
 accumulator_set<unsigned int, stats<tag::lazy_rolling_mean, tag::count>>
 acc(tag::rolling_window::window_size = 3);
 }}}

 Apparently, there are two implementation for rolling_mean and only one of
 them (immediate_rolling_mean, which - unfortunately - is the default) is
 affected by this issue.

 As a quick fix, a
 {{{static_assert(!std::is_unsigned<Sample>::value, "");}}}
 in immediate_rolling_mean_impl (statistics/rolling_mean.hpp) would prevent
 at least some troblesome use cases.
 However, even then it is possible to get incorrect results, if an unsigned
 type is fed to a signed accumulator_set. Due to C++ type promotion rules,
 a mixed signed/unsigned subtraction inside immediate_rolling_mean_impl
 would be performed as an unsigned operation and may underflow, causing
 weird behavior as described above.

 To summarize, immediate_rolling_mean_impl needs some careful fixing. Until
 then it is probably better to make lazy_rolling_mean the default.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/11437#comment:2>
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:19 UTC