#include #include #include #include #include typedef boost::multiprecision::number > boost_quadfloat_t; double my_convert_to_double(const boost_quadfloat_t& x) { if (isnormal(x)) { if (x.backend().exponent() < -1023+1) { // subnormal or zero if (x.backend().exponent() < -1023 - 52) { return x.backend().sign() ? -0.0 : 0.0; // underflow } double adj = x.backend().sign() ? -DBL_MIN : DBL_MIN; boost_quadfloat_t tmp = x + adj; return tmp.convert_to() - adj; } } return x.convert_to(); } int main(int , char** ) { boost_quadfloat_t b = ldexp(boost_quadfloat_t(0x8000000000000bffull), -63 - 1023); double d0 = b.convert_to(); double d1 = std::nexttoward(d0, d0 < b ? DBL_MAX : -DBL_MAX); std::cout << std::setprecision(21) << b << " = b\n" << d0 << " = d0 = b.convert_to()\n" << d1 << " = d1 = std::nexttoward(d0, ...)\n" << abs(d0 - b) << " = abs(d0 - b)\n" << abs(d1 - b) << " = abs(d1 - b)\n" << ((abs(d1 - b) < abs(d0 - b)) ? "d0 is not the closest representable double!!!\n" : "o.k.\n") ; d0 = my_convert_to_double(b); d1 = std::nexttoward(d0, d0 < b ? DBL_MAX : -DBL_MAX); std::cout << d0 << " = d0 = my_convert_to_double(b)\n" << d1 << " = d1 = std::nexttoward(d0, ...)\n" << abs(d0 - b) << " = abs(d0 - b)\n" << abs(d1 - b) << " = abs(d1 - b)\n" << ((abs(d1 - b) < abs(d0 - b)) ? "d0 is not the closest representable double!!!\n" : "o.k.\n") ; return 0; }