#include #include #include #include #include typedef boost::multiprecision::number > boost_quadfloat_t; float my_convert_to_float(const boost_quadfloat_t& x) { if (isnormal(x)) { if (x.backend().exponent() < -127+1) { // subnormal or zero if (x.backend().exponent() < -127 - 23) { return x.backend().sign() ? -0.0f : 0.0f; // underflow } float adj = x.backend().sign() ? -FLT_MIN : FLT_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(0x8000017f), -31 - 127); float f0 = b.convert_to(); float f1 = std::nexttoward(f0, f0 < b ? FLT_MAX : -FLT_MAX); std::cout << std::setprecision(11) << b << " = b\n" << f0 << " = f0 = b.convert_to()\n" << f1 << " = f1 = std::nexttoward (f0, ...)\n" << abs(f0 - b) << " = abs(f0 - b)\n" << abs(f1 - b) << " = abs(f1 - b)\n" << ((abs(f1 - b) < abs(f0 - b)) ? "f0 is not the closest representable float!!!\n" : "o.k.\n") ; f0 = my_convert_to_float(b); f1 = std::nexttoward (f0, f0 < b ? FLT_MAX : -FLT_MAX); std::cout << f0 << " = f0 = my_convert_to_float(b)\n" << f1 << " = f1 = std::nexttoward (f0, ...)\n" << abs(f0 - b) << " = abs(f0 - b)\n" << abs(f1 - b) << " = abs(f1 - b)\n" << ((abs(f1 - b) < abs(f0 - b)) ? "f0 is not the closest representable float!!!\n" : "o.k.\n") ; return 0; }