Boost C++ Libraries: Ticket #10085: Improve cpp_rational::convert_to<double> https://svn.boost.org/trac10/ticket/10085 <p> Hello, </p> <p> the following program prints -nan. If I convert to long double, it prints 1 as expected, but the same problem happens for slightly bigger numerators and denominators. Worse, I can make it return inf or 0 for numbers close to 10 or 0.1. Instead of converting numerator and denominator to double and dividing, it would work better if you had some kind of frexp function that you could apply to both, so the exponents could cancel out without overflowing (that might actually be the same as converting to a cpp_float&lt;53&gt; if it existed). </p> <p> By the way, it would be nice to document what rounding we can expect from the conversion to double. For cpp_int, can I assume the result is within 1 ulp, rounded towards 0? .5 ulp, rounded to nearest? My goal is to get an interval of double that contains the number (convert_to&lt;boost::numeric::interval_lib::interval&lt;double,P&gt;&gt; basically, except that I don't use boost's type for intervals). </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;limits&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/multiprecision/cpp_int.hpp&gt;</span><span class="cp"></span> <span class="k">typedef</span> <span class="n">boost</span><span class="o">::</span><span class="n">multiprecision</span><span class="o">::</span><span class="n">cpp_rational</span> <span class="n">NT</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">(){</span> <span class="n">NT</span> <span class="n">x</span> <span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">numeric_limits</span><span class="o">&lt;</span><span class="kt">double</span><span class="o">&gt;::</span><span class="n">min</span><span class="p">());</span> <span class="n">x</span> <span class="o">*=</span> <span class="n">x</span><span class="p">;</span> <span class="n">x</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">x</span><span class="p">.</span><span class="n">convert_to</span><span class="o">&lt;</span><span class="kt">double</span><span class="o">&gt;</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="sc">&#39;\n&#39;</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><p> PS: thank you for the quick resolution to the previous ticket! </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10085 Trac 1.4.3 John Maddock Tue, 03 Jun 2014 07:51:54 GMT <link>https://svn.boost.org/trac10/ticket/10085#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10085#comment:1</guid> <description> <p> That's exposed a number of conversion related issues that should be handled better, bare with me, the fix isn't so trivial this time... </p> </description> <category>Ticket</category> </item> <item> <author>marc.glisse@…</author> <pubDate>Tue, 03 Jun 2014 17:50:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10085#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10085#comment:2</guid> <description> <p> Thanks. Indeed the other ticket was more political and this one is more technical. Don't worry about doing this soon, it doesn't have to be in 1.56 or 1.57. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Sat, 28 Jun 2014 16:15:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10085#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10085#comment:3</guid> <description> <p> This should be mostly fixed in this feature branch: <a class="ext-link" href="https://github.com/boostorg/multiprecision/tree/Issue_10085"><span class="icon">​</span>https://github.com/boostorg/multiprecision/tree/Issue_10085</a> </p> <p> Conversions to and from native floating point types and multiprecision ones are supported, conversions to float from rational should be exactly rounded (less than 0.5ulp error) as long as the floating point type is binary and the integer type used by the rational is unbounded. </p> <p> I plan to do some more testing, and possibly fix some other rational-related issues that have been nagging at me before merging to develop, so it's unlikely at present that this makes 1.56... though you never know. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Mon, 30 Jun 2014 16:40:45 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/10085#comment:4 https://svn.boost.org/trac10/ticket/10085#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> This should now be fixed in develop. </p> Ticket