Boost C++ Libraries: Ticket #8133: multiprecision, failed gcd tests (test_cpp_int.cpp) https://svn.boost.org/trac10/ticket/8133 <p> I modified test_cpp_int.cpp to generate random numbers with a lot of ones or zeros. </p> <p> I modified <strong>T generate_random(unsigned bits_wanted)</strong>: </p> <pre class="wiki"> ... T val = 0; for(unsigned i = 0; i &lt; terms_needed; ++i) { val *= (gen.max)(); switch (gen() % 5) { case 0: val += gen(); break; case 1: val += 1; break; case 2: val += (gen.max)() - 2; break; case 3: val += (gen.max)() - 1; break; } } val %= max_val; return (val == 0)? val : 1; } </pre><p> This caused some tests related to gcd computation to fail. Here is output (I removed some parts of it with results of operations) - <a class="ext-link" href="http://pastebin.com/iZdmX1bq"><span class="icon">​</span>http://pastebin.com/iZdmX1bq</a> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8133 Trac 1.4.3 John Maddock Thu, 21 Feb 2013 13:54:27 GMT <link>https://svn.boost.org/trac10/ticket/8133#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8133#comment:1</guid> <description> <p> Thanks for the report - However I'm having trouble reproducing here, note that your random generator code above either returns 0 or 1, I assume the last line should read: </p> <pre class="wiki">return (val == 0) ? 1 : val; </pre><p> ? </p> <p> However, even then neither random numbers generated as above, nor the specific test values printed out in your log trigger errors for me. What compiler/platform is this? I'm testing on Win32 VC10 here, but I can switch to 64-bit Linux if that's the thing I'm missing. </p> <p> BTW the report you outputted, contains line numbers that don't match up to anything in test_cpp_int.cpp - I guess because you modified that file? </p> </description> <category>Ticket</category> </item> <item> <author>Stepan Podoskin <stepik-777@…></author> <pubDate>Thu, 21 Feb 2013 14:46:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8133#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8133#comment:2</guid> <description> <p> Yes, you are right, it should be (val == 0) ? 1 : val; </p> <p> This line is there because tests will fail with division by zero otherwise. </p> <p> I'm testing it on 32-bit Windows with GCC 4.7.2 and MSVC 2010. I'm using boost 1.53.0, not the latest trunk. Following program outputs 1 1 1 3 when compiled with GCC and 3 3 1 3 and when complied with MSVC. </p> <pre class="wiki"> #include &lt;iostream&gt; #include &lt;boost/multiprecision/cpp_int.hpp&gt; using boost::multiprecision::cpp_int; using boost::multiprecision::gcd; int main() { cpp_int a("0xffffffee00000095fffffd0000000a8fffffe4e10000348bffffb1a100005ae3ffffade700003955ffffe19900000bd2fffffcdb00000076fffffffffffffffd"); // correct gcd is 1 std::cout &lt;&lt; gcd(a, 4294967295) &lt;&lt; '\n'; std::cout &lt;&lt; gcd(4294967295, a) &lt;&lt; '\n'; std::cout &lt;&lt; gcd(a, cpp_int("4294967295")) &lt;&lt; '\n'; std::cout &lt;&lt; gcd(cpp_int("4294967295"), a) &lt;&lt; '\n'; return 0; } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Thu, 21 Feb 2013 18:36:18 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8133#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8133#comment:3</guid> <description> <p> Thanks, reproduced. </p> <p> It's a bug in the subtraction routine for subtracting an unsigned int - wrongly uses a carry when subtracting ~static_cast&lt;unsigned&gt;(0). </p> <p> Testing a fix now. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Fri, 22 Feb 2013 10:37:38 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8133#comment:4 https://svn.boost.org/trac10/ticket/8133#comment:4 <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">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/83080" title="Fix bug in subtraction of a limb_type. Fix bug in division/modulus ...">[83080]</a>) Fix bug in subtraction of a limb_type. Fix bug in division/modulus algorithms that results in incorrect sign when source and destination overlap. Tweak performance of GCD algorithms. Add test cases for bug reports. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8133" title="#8133: Bugs: multiprecision, failed gcd tests (test_cpp_int.cpp) (closed: fixed)">#8133</a>. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8126" title="#8126: Bugs: multiprecision, incorrect sign (closed: fixed)">#8126</a>. </p> Ticket