Boost C++ Libraries: Ticket #11437: rolling_mean returns incorrect result when using unsigned int https://svn.boost.org/trac10/ticket/11437 <p> Using rolling_mean with a sample type of "unsigned int" leads to incorrect results. The following example outputs 1.43166e+009 instead of the expected 1. </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;boost/accumulators/accumulators.hpp&gt; #include &lt;boost/accumulators/statistics/stats.hpp&gt; #include &lt;boost/accumulators/statistics/count.hpp&gt; #include &lt;boost/accumulators/statistics/rolling_mean.hpp&gt; using namespace boost::accumulators; int main(int argc, char** argv) { accumulator_set&lt;unsigned int, stats&lt;tag::rolling_mean, tag::count&gt;&gt; acc(tag::rolling_window::window_size = 3); acc(3); acc(2); acc(1); acc(0); std::cout &lt;&lt; rolling_mean(acc) &lt;&lt; std::endl; } </pre><p> The same problem happens if the sample type is "int" but you pass unsigned ints to the accumulator. For example, if you replace the above code with the following code, the result is the same: </p> <pre class="wiki"> accumulator_set&lt;int, stats&lt;tag::rolling_mean, tag::count&gt;&gt; acc(tag::rolling_window::window_size = 3); acc(3U); acc(2U); acc(1U); acc(0U); </pre><p> I found this problem using Visual Studio 2010, after upgrading from Boost 1.44.0 to 1.58.0. With Boost 1.44.0, the above examples return 1. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11437 Trac 1.4.3 aholaway@… Tue, 06 Oct 2015 13:37:05 GMT <link>https://svn.boost.org/trac10/ticket/11437#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11437#comment:1</guid> <description> <p> I'm also experiencing this issue after upgrading from Boost 1.49 to 1.58. I can work around the issue by using signed integers, but it would be nice to not have to. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Martin</dc:creator> <pubDate>Wed, 13 Jan 2016 18:50:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11437#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11437#comment:2</guid> <description> <p> Another workaround, which allows sticking with unsigned types, is to declare the accumulator_set using lazy_rolling_mean: </p> <pre class="wiki">accumulator_set&lt;unsigned int, stats&lt;tag::lazy_rolling_mean, tag::count&gt;&gt; acc(tag::rolling_window::window_size = 3); </pre><p> 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. </p> <p> As a quick fix, a <code>static_assert(!std::is_unsigned&lt;Sample&gt;::value, "");</code> 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. </p> <p> To summarize, immediate_rolling_mean_impl needs some careful fixing. Until then it is probably better to make lazy_rolling_mean the default. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Arjun</dc:creator> <pubDate>Tue, 23 Aug 2016 12:10:37 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11437#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11437#comment:3</guid> <description> <p> what are the performance and memory implications of using lazy_rolling_mean vs immediate_rolling_mean? </p> </description> <category>Ticket</category> </item> <item> <author>ojford@…</author> <pubDate>Wed, 07 Feb 2018 10:16:33 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11437#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11437#comment:4</guid> <description> <p> I've fixed this and added a PR at <a class="ext-link" href="https://github.com/boostorg/accumulators/pull/14"><span class="icon">​</span>https://github.com/boostorg/accumulators/pull/14</a> </p> </description> <category>Ticket</category> </item> </channel> </rss>