Boost C++ Libraries: Ticket #2671: Calculation overflow for the 4th moment https://svn.boost.org/trac10/ticket/2671 <p> While testing the accumulator library the calculation of the kurtosis goes wrong with a larger dataset. I traced to problem down to the calculation of the 4th moment which seems to overflow. The attached program produces the following output on a 32 bit machine. </p> <pre class="wiki">2.44141e+08 2.48094e+08 2.52111e+08 2.56192e+08 2.60338e+08 2.6455e+08 2.68829e+08 2.73175e+08 2.77589e+08 2.82072e+08 2.86624e+08 2.91247e+08 2.95942e+08 3.00708e+08 1.92167e+07 4.20257e+07 6.28038e+07 8.19029e+07 9.96007e+07 1.1612e+08 1.3164e+08 1.4631e+08 1.60251e+08 1.73565e+08 1.45388e+07 </pre><p> PS I didn't test whether the 3th moment also can overflow, but I had no problem with the calculation of the skewness. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2671 Trac 1.4.3 m.de.wever@… Thu, 22 Jan 2009 15:11:56 GMT attachment set https://svn.boost.org/trac10/ticket/2671 https://svn.boost.org/trac10/ticket/2671 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test.cpp</span> </li> </ul> <p> test program to reproduce the problem </p> Ticket Steven Watanabe Sat, 24 Jan 2009 20:10:07 GMT <link>https://svn.boost.org/trac10/ticket/2671#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2671#comment:1</guid> <description> <p> Of course it overflows if you use unsigned int. Use double instead. </p> </description> <category>Ticket</category> </item> <item> <author>m.de.wever@…</author> <pubDate>Wed, 04 Feb 2009 09:20:55 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2671#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2671#comment:2</guid> <description> <p> I did some more tests and the 4th moment returns a double when I use unsigned as template type. So I still consider it a bug. </p> <p> This example, from the library documentation, also leads me to believe the result type is a floating point type and it's save to use integer types. </p> <pre class="wiki">accumulator_set&lt;int, stats&lt;tag::moment&lt;2&gt; &gt; &gt; acc1; acc1(2); // 4 acc1(4); // 16 acc1(5); // + 25 // = 45 / 3 = 15 BOOST_CHECK_CLOSE(15., moment&lt;2&gt;(acc1), 1e-5); </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Eric Niebler</dc:creator> <pubDate>Wed, 04 Feb 2009 17:13:57 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2671#comment:3 https://svn.boost.org/trac10/ticket/2671#comment:3 <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">wontfix</span> </li> </ul> <p> The moment returns a double even for integral sample types because of the division at the end. The additions during the accumulation phase are integral, and so they overflow. Adding overflow checks would slow down the common case, so I'm disinclined to add it unconditionally. Steven is right, the easiest solution is for you to use doubles. </p> <p> The larger issue is how to handle things like overflow, underflow, divide-by-zero, NaN propagation, etc., throughout the Accumulators library (and beyond to all the math-related libraries in Boost (and beyond to the standard library)). One possibility would be to add checked arithmetic as a policy. Another possibility is to specify the sample type as checked&lt;int&gt; or something. It's probably worth starting such a discussion on the Boost list. I'm sure John Maddock would have some useful insights. </p> <p> I'm resolving this bug as "wontfix" and will open a separate feature request ticket for better control over error-handling in Accumulators. </p> Ticket m.de.wever@… Fri, 13 Feb 2009 08:03:22 GMT <link>https://svn.boost.org/trac10/ticket/2671#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2671#comment:4</guid> <description> <p> I tested with the double and it works properly now. </p> <p> Thanks for opening a feature request. I think it would also be nice to add a warning about the overflow problem in the documentation until that feature has been implemented. </p> </description> <category>Ticket</category> </item> </channel> </rss>