Boost C++ Libraries: Ticket #9579: conversion error from boost cpp_rational to int https://svn.boost.org/trac10/ticket/9579 <p> boost cpp_rational seems to convert wrong to int when the numerator and denominator have many digits. </p> <pre class="wiki"> #include &lt;iostream&gt; #include &lt;boost/multiprecision/cpp_int.hpp&gt; using namespace boost::multiprecision; using namespace std; int main() { cpp_rational a("4561231231235/123123123123"); std::cout &lt;&lt; "bad convert: " &lt;&lt; a &lt;&lt; ' ' &lt;&lt; float(a) &lt;&lt; ' ' &lt;&lt; int(a) &lt;&lt; ' ' &lt;&lt; a.convert_to&lt;int&gt;() &lt;&lt; endl; a = (cpp_rational)"456/123"; std::cout &lt;&lt; "good convert: " &lt;&lt; a &lt;&lt; ' ' &lt;&lt; float(a) &lt;&lt; ' ' &lt;&lt; int(a) &lt;&lt; ' ' &lt;&lt; a.convert_to&lt;int&gt;() &lt;&lt; endl; } </pre><p> The output is: </p> <pre class="wiki"> bad convert: 651604461605/17589017589 37.0461 -3 -3 good convert: 152/41 3.70732 3 3 </pre><p> Also, attempts to convert cpp_rational to cpp_int fail to compile, e.g., using </p> <pre class="wiki"> cpp_int b = static_cast&lt;cpp_int&gt; (a); cpp_int b = a.convert_to&lt;cpp_int&gt;(); </pre><p> What I want to happen is to divide and round down and never get it wrong even close to an int. </p> <p> Help? Thanks. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9579 Trac 1.4.3 j@… Thu, 16 Jan 2014 04:59:29 GMT owner, component changed https://svn.boost.org/trac10/ticket/9579#comment:1 https://svn.boost.org/trac10/ticket/9579#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Jonathan Turkanis</span> to <span class="trac-author">John Maddock</span> </li> <li><strong>component</strong> <span class="trac-field-old">rational</span> → <span class="trac-field-new">multiprecision</span> </li> </ul> <p> I think you have selected the wrong component. I am going to change it to multiprecision. </p> Ticket anonymous Thu, 16 Jan 2014 13:01:20 GMT <link>https://svn.boost.org/trac10/ticket/9579#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9579#comment:2</guid> <description> <p> I'm testing a proper fix, but the workaround would be to use: </p> <p> <code>cpp_int i = numerator(a) / denominator(a);</code> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 17 Jan 2014 08:17:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9579#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9579#comment:3</guid> <description> <p> Thanks, am using that. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Fri, 17 Jan 2014 10:08:27 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/9579#comment:4 https://svn.boost.org/trac10/ticket/9579#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> The fix has gone into Git develop: <a class="ext-link" href="https://github.com/boostorg/multiprecision/commit/9a05e24994e268035184df35643b690c1c749cdc"><span class="icon">​</span>https://github.com/boostorg/multiprecision/commit/9a05e24994e268035184df35643b690c1c749cdc</a> </p> Ticket anonymous Mon, 20 Jan 2014 16:08:36 GMT <link>https://svn.boost.org/trac10/ticket/9579#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9579#comment:5</guid> <description> <p> Thanks! </p> </description> <category>Ticket</category> </item> </channel> </rss>