# [Boost-bugs] [Boost C++ Libraries] #10085: Improve cpp_rational::convert_to<double>

Subject: [Boost-bugs] [Boost C++ Libraries] #10085: Improve cpp_rational::convert_to<double>
Date: 2014-06-01 20:29:41

#10085: Improve cpp_rational::convert_to<double>
------------------------------+----------------------------
Type: Feature Requests | Status: new
Milestone: To Be Determined | Component: multiprecision
Version: Boost 1.55.0 | Severity: Problem
Keywords: |
------------------------------+----------------------------
Hello,

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<53> if it existed).

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<boost::numeric::interval_lib::interval<double,P>> basically,
except that I don't use boost's type for intervals).

{{{#!c++
#include <iostream>
#include <limits>
#include <boost/multiprecision/cpp_int.hpp>
typedef boost::multiprecision::cpp_rational NT;

int main(){
NT x (std::numeric_limits<double>::min());
x *= x;
x += 1;
std::cout << x.convert_to<double>() << '\n';
}
}}}

PS: thank you for the quick resolution to the previous ticket!

```--
Ticket URL: <https://svn.boost.org/trac/boost/ticket/10085>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.
```

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:16 UTC