Boost C++ Libraries: Ticket #2228: signed overflow problem in weighted_sum on VC9.0 https://svn.boost.org/trac10/ticket/2228 <p> Hi, </p> <p> I have spotted rather unexpected behaviour while using weighted_sum. </p> <p> Here is the simple code: </p> <pre class="wiki">// use namespaces using namespace boost::accumulators; using namespace std; // create a int accumulator with std::size_t weight accumulator_set&lt;int, features&lt;tag::weighted_sum&gt;, std::size_t&gt; Accum; // Accumulate negative value with positive weight Accum(-56, boost::accumulators::weight=3); // Print the output wcout &lt;&lt; "sum: " &lt;&lt; boost::accumulators::weighted_sum(Accum) &lt;&lt; endl; </pre><p> I was expecting to get result -168, however I got 4294967128. </p> <p> I have traced the problem to weighted_sum_impl. </p> <p> I don't know why, but </p> <pre class="wiki">weighted_sum_impl&lt;int, unsigned int, tag::sample&gt;::weighted_sample </pre><p> is resolved to <code>unsigned int</code>. I think this is plain wrong. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2228 Trac 1.4.3 Eric Niebler Tue, 19 Aug 2008 16:06:04 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2228#comment:1 https://svn.boost.org/trac10/ticket/2228#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> This is not a bug. The sample is multiplied by the weight. In C++, and int multiplied by an unsigned int is an unsigned int. (Try it and see.) </p> <p> If you want the result to be an int, use an int for the weight type. </p> Ticket Pavol Droba Tue, 19 Aug 2008 18:02:37 GMT <link>https://svn.boost.org/trac10/ticket/2228#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2228#comment:2</guid> <description> <p> I understand that the current solution is technically correct. However I think that conceptually it is not. </p> <p> An example: To calculate a weighted mean of signed integers, where weight represents sample cardinality, I would consider it quite natural to expect that the mean will be signed, since I'm accumulating signed integers. Even though the cardinality is natural (i.e. unsigned) number. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Eric Niebler</dc:creator> <pubDate>Tue, 19 Aug 2008 18:27:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2228#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2228#comment:3</guid> <description> <p> I understand your point, but I still disagree. I'm not going to get into guessing what a "better" type to hold the result is. It can be argued from the other side ... that using an int to hold the result of an (int*unsigned) can overflow and wrap. There is no good solution. Following the rules of C++ is simply the best (as in least surprising, easiest to explain) thing to do. </p> </description> <category>Ticket</category> </item> </channel> </rss>