T float_advance(T val, int distance, const Policy& pol) { // // Error handling: // static const char* function = "float_advance<%1%>(%1%, int)"; if(!(boost::math::isfinite)(val)) return policies::raise_domain_error<T>( function, "Argument val must be finite, but got %1%", val, pol); if(val < 0) return -float_advance(-val, -distance, pol); if(distance == 0) return val; if(distance == 1) return float_next(val, pol); if(distance == -1) return float_prior(val, pol); BOOST_MATH_STD_USING int expon; frexp(val, &expon); T limit = ldexp((distance < 0 ? T(0.5f) : T(1)), expon); if(val <= tools::min_value<T>()) { limit = sign(T(distance)) * tools::min_value<T>(); } T limit_distance = float_distance(val, limit); while(fabs(limit_distance) < abs(distance)) { distance -= itrunc(limit_distance); val = limit; if(distance < 0) { limit /= 2; expon--; } else { limit *= 2; expon++; } limit_distance = float_distance(val, limit); } if((0.5f == frexp(val, &expon)) && (distance < 0)) --expon; T diff = 0; if(val != 0) diff = distance * ldexp(T(1), expon - tools::digits<T>()); if(diff == 0) diff = distance * detail::get_smallest_value<T>(); return val += diff; }
T float_advance(T val, int distance, const Policy& pol) { BOOST_MATH_STD_USING // // Error handling: // static const char* function = "float_advance<%1%>(%1%, int)"; int fpclass = (pdalboost::math::fpclassify)(val); if((fpclass == FP_NAN) || (fpclass == FP_INFINITE)) return policies::raise_domain_error<T>( function, "Argument val must be finite, but got %1%", val, pol); if(val < 0) return -float_advance(-val, -distance, pol); if(distance == 0) return val; if(distance == 1) return float_next(val, pol); if(distance == -1) return float_prior(val, pol); if(fabs(val) < detail::get_min_shift_value<T>()) { // // Special case: if the value of the least significant bit is a denorm, // implement in terms of float_next/float_prior. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set. // if(distance > 0) { do{ val = float_next(val, pol); } while(--distance); } else { do{ val = float_prior(val, pol); } while(++distance); } return val; } int expon; frexp(val, &expon); T limit = ldexp((distance < 0 ? T(0.5f) : T(1)), expon); if(val <= tools::min_value<T>()) { limit = sign(T(distance)) * tools::min_value<T>(); } T limit_distance = float_distance(val, limit); while(fabs(limit_distance) < abs(distance)) { distance -= itrunc(limit_distance); val = limit; if(distance < 0) { limit /= 2; expon--; } else { limit *= 2; expon++; } limit_distance = float_distance(val, limit); if(distance && (limit_distance == 0)) { policies::raise_evaluation_error<T>(function, "Internal logic failed while trying to increment floating point value %1%: most likely your FPU is in non-IEEE conforming mode.", val, pol); } } if((0.5f == frexp(val, &expon)) && (distance < 0)) --expon; T diff = 0; if(val != 0) diff = distance * ldexp(T(1), expon - tools::digits<T>()); if(diff == 0) diff = distance * detail::get_smallest_value<T>(); return val += diff; }
T float_distance(const T& a, const T& b, const Policy& pol) { BOOST_MATH_STD_USING // // Error handling: // static const char* function = "float_distance<%1%>(%1%, %1%)"; if(!(boost::math::isfinite)(a)) return policies::raise_domain_error<T>( function, "Argument a must be finite, but got %1%", a, pol); if(!(boost::math::isfinite)(b)) return policies::raise_domain_error<T>( function, "Argument b must be finite, but got %1%", b, pol); // // Special cases: // if(a > b) return -float_distance(b, a); if(a == b) return 0; if(a == 0) return 1 + fabs(float_distance(boost::math::sign(b) * detail::get_smallest_value<T>(), b, pol)); if(b == 0) return 1 + fabs(float_distance(boost::math::sign(a) * detail::get_smallest_value<T>(), a, pol)); if(boost::math::sign(a) != boost::math::sign(b)) return 2 + fabs(float_distance(boost::math::sign(b) * detail::get_smallest_value<T>(), b, pol)) + fabs(float_distance(boost::math::sign(a) * detail::get_smallest_value<T>(), a, pol)); // // By the time we get here, both a and b must have the same sign, we want // b > a and both postive for the following logic: // if(a < 0) return float_distance(-b, -a); BOOST_ASSERT(a >= 0); BOOST_ASSERT(b >= a); BOOST_MATH_STD_USING int expon; // // Note that if a is a denorm then the usual formula fails // because we actually have fewer than tools::digits<T>() // significant bits in the representation: // frexp(((boost::math::fpclassify)(a) == FP_SUBNORMAL) ? tools::min_value<T>() : a, &expon); T upper = ldexp(T(1), expon); T result = 0; expon = tools::digits<T>() - expon; // // If b is greater than upper, then we *must* split the calculation // as the size of the ULP changes with each order of magnitude change: // if(b > upper) { result = float_distance(upper, b); } // // Use compensated double-double addition to avoid rounding // errors in the subtraction: // T mb = -(std::min)(upper, b); T x = a + mb; T z = x - a; T y = (a - (x - z)) + (mb - z); if(x < 0) { x = -x; y = -y; } result += ldexp(x, expon) + ldexp(y, expon); // // Result must be an integer: // BOOST_ASSERT(result == floor(result)); return result; }
T float_distance(const T& a, const T& b, const Policy& pol) { BOOST_MATH_STD_USING // // Error handling: // static const char* function = "float_distance<%1%>(%1%, %1%)"; if(!(pdalboost::math::isfinite)(a)) return policies::raise_domain_error<T>( function, "Argument a must be finite, but got %1%", a, pol); if(!(pdalboost::math::isfinite)(b)) return policies::raise_domain_error<T>( function, "Argument b must be finite, but got %1%", b, pol); // // Special cases: // if(a > b) return -float_distance(b, a, pol); if(a == b) return 0; if(a == 0) return 1 + fabs(float_distance(static_cast<T>((b < 0) ? -detail::get_smallest_value<T>() : detail::get_smallest_value<T>()), b, pol)); if(b == 0) return 1 + fabs(float_distance(static_cast<T>((a < 0) ? -detail::get_smallest_value<T>() : detail::get_smallest_value<T>()), a, pol)); if(pdalboost::math::sign(a) != pdalboost::math::sign(b)) return 2 + fabs(float_distance(static_cast<T>((b < 0) ? -detail::get_smallest_value<T>() : detail::get_smallest_value<T>()), b, pol)) + fabs(float_distance(static_cast<T>((a < 0) ? -detail::get_smallest_value<T>() : detail::get_smallest_value<T>()), a, pol)); // // By the time we get here, both a and b must have the same sign, we want // b > a and both postive for the following logic: // if(a < 0) return float_distance(static_cast<T>(-b), static_cast<T>(-a), pol); BOOST_ASSERT(a >= 0); BOOST_ASSERT(b >= a); int expon; // // Note that if a is a denorm then the usual formula fails // because we actually have fewer than tools::digits<T>() // significant bits in the representation: // frexp(((pdalboost::math::fpclassify)(a) == FP_SUBNORMAL) ? tools::min_value<T>() : a, &expon); T upper = ldexp(T(1), expon); T result = 0; expon = tools::digits<T>() - expon; // // If b is greater than upper, then we *must* split the calculation // as the size of the ULP changes with each order of magnitude change: // if(b > upper) { result = float_distance(upper, b); } // // Use compensated double-double addition to avoid rounding // errors in the subtraction: // T mb, x, y, z; if(((pdalboost::math::fpclassify)(a) == FP_SUBNORMAL) || (b - a < tools::min_value<T>())) { // // Special case - either one end of the range is a denormal, or else the difference is. // The regular code will fail if we're using the SSE2 registers on Intel and either // the FTZ or DAZ flags are set. // T a2 = ldexp(a, tools::digits<T>()); T b2 = ldexp(b, tools::digits<T>()); mb = -(std::min)(T(ldexp(upper, tools::digits<T>())), b2); x = a2 + mb; z = x - a2; y = (a2 - (x - z)) + (mb - z); expon -= tools::digits<T>(); } else { mb = -(std::min)(upper, b); x = a + mb; z = x - a; y = (a - (x - z)) + (mb - z); } if(x < 0) { x = -x; y = -y; } result += ldexp(x, expon) + ldexp(y, expon); // // Result must be an integer: // BOOST_ASSERT(result == floor(result)); return result; }