Boost C++ Libraries: Ticket #567: rational.hpp::gcd returns a negative value sometimes https://svn.boost.org/trac10/ticket/567 <pre class="wiki">boost/rational.hpp provides a gcd function, which is supposed to be non-negative. It sometimes returns a negative value, which can cause problems in other parts of rational.hpp. For example, assuming long is a 4-byte type, boost::gcd&lt;long&gt;(6, -2147483648) returns -2. As a result, boost::rational&lt;long&gt;(-1073741821, 6) + boost::rational&lt;long&gt;(-1073741827, 6) produces an invalid rational number, 1073741824/-3, instead of the correct answer -1073741824/3. Here is a small patch to fix the problem. Note that this is how boost/math/common_factor_rt.hpp calculates the greatest common divisor. </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/567 Trac 1.4.3 Daryle Walker Sat, 04 Nov 2006 21:27:41 GMT status changed https://svn.boost.org/trac10/ticket/567#comment:1 https://svn.boost.org/trac10/ticket/567#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 Fixed this by depreciating boost::gcd and lcm for the ones in boost::math (i.e. in "common_factor_rt.hpp"). This forces the requirement of std::numeric_limits compatibility for your integer type (otherwise you may get negative numbers during GCD/LCM for signed types). Your issue is only for a "long" that uses 2's complement and all its bit combinations, so there's a preprocessor-level check for that in the test code. </pre> Ticket