Boost C++ Libraries: Ticket #2737: Rounding failed for -0.5 https://svn.boost.org/trac10/ticket/2737 <p> using namespace boost::numeric; typedef int T; typedef double S; typedef conversion_traits&lt;T, S&gt; Traits; int i = converter&lt;T, S, Traits, def_overflow_handler, <a class="missing wiki">RoundEven</a>&lt;Traits::source_type&gt; &gt;::convert(-0.5); </p> <p> When I run the code above, -0.5 will be converted to 0, but I expect -0.5 to be converted to -1. The same problem goes for 0.5 as well. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2737 Trac 1.4.3 Steven Watanabe Tue, 10 Feb 2009 03:06:16 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2737#comment:1 https://svn.boost.org/trac10/ticket/2737#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> The library's behavior is correct. Round to even returns the nearest integer or if both adjacent integers are equidistant returns the even one. </p> Ticket anonymous Tue, 10 Feb 2009 04:06:22 GMT <link>https://svn.boost.org/trac10/ticket/2737#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2737#comment:2</guid> <description> <p> Sorry. I didn't realize that for rounding, common method and round-to-even method are different. Is there a way to do rounding in common method? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Tue, 10 Feb 2009 16:14:59 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2737#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2737#comment:3</guid> <description> <p> The common method is not provided. Round to even has better statistical properties. It would look something like this based on <a class="missing wiki">RoundEven</a> </p> <pre class="wiki">template&lt;class S&gt; struct RoundCommon { typedef S source_type ; typedef typename mpl::if_&lt; is_arithmetic&lt;S&gt;,S,S const&amp;&gt;::type argument_type ; static source_type nearbyint ( argument_type s ) { #if !defined(BOOST_NO_STDC_NAMESPACE) using std::floor ; using std::ceil ; #endif // only works inside the range not at the boundaries S prev = floor(s); S next = ceil(s); S rt = (s - prev) - (next - s); // remainder type S const zero(0.0); S const two(2.0); if ( rt &lt; zero ) return prev; else if ( rt &gt; zero ) return next; else if ( prev &lt; zero ) return prev; else return next; } typedef mpl::integral_c&lt; std::float_round_style, std::round_to_nearest&gt; round_style ; } ; </pre> </description> <category>Ticket</category> </item> </channel> </rss>