# Re: [Boost-bugs] [Boost C++ Libraries] #12327: cpp_rational::convert_to<double>() does not return the nearest number

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #12327: cpp_rational::convert_to<double>() does not return the nearest number
Date: 2016-07-15 04:46:21

#12327: cpp_rational::convert_to<double>() does not return the nearest number
-------------------------------+----------------------------
Type: Bugs | Status: new
Milestone: To Be Determined | Component: multiprecision
Version: Boost 1.61.0 | Severity: Problem
Resolution: | Keywords:
-------------------------------+----------------------------

Comment (by komakisen@â€¦):

> Then r112 becomes 5404319552844595/54043195528445952 which is
0.0999999999999999962992565845828115319212277730305989583... in decimal,
which rounds up to 0.10000000000000001 and not down to
0.099999999999999992 due to the "6" after the string of 9's.

Thank you for investigating my program, but I still don't understand this
point.

To begin with, 0.099999999999999992 is actually
0.09999999999999999167332... and 0.10000000000000001 is actually
0.1000000000000000055511... (they differ by 1ulp)

So 0.09999999999999999629925... is less than their midpoint
0.09999999999999999861222...

Also, `double` is a binary floating-point number. Thus I believe round-off
calculation on a decimal digit "6" explains nothing here.

If I run the following program, I get 0.099999999999999992.

{{{#!c++
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
double d = 0.09999999999999999629926;
std::cout << std::setprecision(17) << d << std::endl;
return 0;
}
}}}

So isn't 0.099999999999999992 the expected result? Or am I confusing
myself?

```--
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12327#comment:2>
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:20 UTC