diff --git a/include/boost/math/special_functions/next.hpp b/include/boost/math/special_functions/next.hpp index 0da7aca475..4336c07e0e 100644 --- a/include/boost/math/special_functions/next.hpp +++ b/include/boost/math/special_functions/next.hpp @@ -725,6 +725,7 @@ typename tools::promote_args::type float_distance(const T& a, const U& b) inline std::int32_t float_distance(float a, float b) { using std::abs; + using std::isfinite; constexpr auto tol = 2 * (std::numeric_limits::min)(); // 0, very small, and large magnitude distances all need special handling @@ -737,6 +738,20 @@ inline std::int32_t float_distance(float a, float b) return static_cast(float_distance(a, b, policies::policy<>())); } + static const char* function = "float_distance<%1%>(%1%, %1%)"; + if(!(boost::math::isfinite)(a)) + { + return policies::raise_domain_error( + function, + "Argument a must be finite, but got %1%", a, policies::policy<>()); + } + if(!(boost::math::isfinite)(b)) + { + return policies::raise_domain_error( + function, + "Argument b must be finite, but got %1%", b, policies::policy<>()); + } + static_assert(sizeof(float) == sizeof(std::int32_t), "float is incorrect size."); std::int32_t ai; @@ -757,6 +772,7 @@ inline std::int32_t float_distance(float a, float b) inline std::int64_t float_distance(double a, double b) { using std::abs; + using std::isfinite; constexpr auto tol = 2 * (std::numeric_limits::min)(); // 0, very small, and large magnitude distances all need special handling @@ -768,6 +784,21 @@ inline std::int64_t float_distance(double a, double b) { return static_cast(float_distance(a, b, policies::policy<>())); } + + static const char* function = "float_distance<%1%>(%1%, %1%)"; + if(!(boost::math::isfinite)(a)) + { + return policies::raise_domain_error( + function, + "Argument a must be finite, but got %1%", a, policies::policy<>()); + } + if(!(boost::math::isfinite)(b)) + { + return policies::raise_domain_error( + function, + "Argument b must be finite, but got %1%", b, policies::policy<>()); + } + static_assert(sizeof(double) == sizeof(std::int64_t), "double is incorrect size.");