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

Subject: [Boost-bugs] [Boost C++ Libraries] #12327: cpp_rational::convert_to<double>() does not return the nearest number
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-07-13 15:20:16


#12327: cpp_rational::convert_to<double>() does not return the nearest number
------------------------------+----------------------------
 Reporter: komakisen@… | Owner: johnmaddock
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: multiprecision
  Version: Boost 1.61.0 | Severity: Problem
 Keywords: |
------------------------------+----------------------------
 Hello,

 I tested with the following program to see if
 boost::multiprecision::cpp_rational::convert_to<double>() returns the
 nearest double number to the exact value. I expected it to print only
 "true"s, but it actually prints some "false"s too. Is it a bug or
 (un)documented behavior?

 {{{#!c++
 #include <boost/multiprecision/cpp_int.hpp>

 typedef boost::multiprecision::cpp_rational NT;

 void test_convert_to_double(double d1, double d2)
 {
    NT r1(d1);
    NT r2(d2);

    NT two(2);
    NT three(3);

    NT r112 = (two * r1 + r2) / three;
    NT r12 = (r1 + r2) / two;
    NT r122 = (r1 + two * r2) / three;

    double rd1 = r1.convert_to<double>();
    double rd112 = r112.convert_to<double>();
    double rd12 = r12.convert_to<double>();
    double rd122 = r122.convert_to<double>();
    double rd2 = r2.convert_to<double>();

    std::cout << std::boolalpha;
    std::cout << (rd1 == d1) << std::endl;
    std::cout << (rd112 == d1) << std::endl;
    std::cout << (rd12 == d2) << std::endl;
    std::cout << (rd122 == d2) << std::endl;
    std::cout << (rd2 == d2) << std::endl;
 }

 int main()
 {
    volatile double a = 0.099999999999999992;
    volatile double b = 0.10000000000000001;
    volatile double c = 0.10000000000000002;

    // prints true, false, true, true, true
    test_convert_to_double(a, b);

    // prints true, true, false, false, true
    test_convert_to_double(b, c);

    return 0;
 }
 }}}

 I compiled this program with MSVC 14.0 and ICL 16.0.3, both for x64 Debug
 build. They give the same results.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12327>
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