Boost C++ Libraries: Ticket #565: rational::operator< fails for unsigned value types https://svn.boost.org/trac10/ticket/565 <pre class="wiki">There's a minor bug in rational.hpp when using unsigned values. Consider the following simple program: int main(void) { boost::rational&lt;unsigned int&gt; x = 0; assert(x.operator&lt;(1)); } The assertion fails. The reason is that in rational.hpp::rational&lt;IntType&gt;::operator&lt; (param_type i) are the lines if (num &gt; zero) return (num/den) &lt; i; else return -i &lt; (-num/den); The last line is bogus for unsigned types if num == 0 and i &gt; 0. The problem can be fixed by changing (num &gt; zero) to (num &gt;= zero). Another issue with the same section of code is that it uses up to 5 comparisons of IntType values. Attached is a patch that fixes the bug described, and also makes the function only perform up to 3 comparisons. That can make a difference, of course, if IntType is a complicated type with a slow comparison operator. </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/565 Trac 1.4.3 Daryle Walker Thu, 02 Nov 2006 16:03:05 GMT status changed https://svn.boost.org/trac10/ticket/565#comment:1 https://svn.boost.org/trac10/ticket/565#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> </ul> <pre class="wiki">Logged In: YES user_id=551024 An extra change while fixing bug #798357 fixes this issue, without using this patch. The current solution performs a floor-rounding division and modulus, followed by a comparison. The floor division involves either one or two extra comparisons, for possibly converting from a truncating division. (The assert currently there uses another comparison.) </pre> Ticket