Skip to content

Commit b847b23

Browse files
committed
Improve error handling
1 parent d405784 commit b847b23

File tree

1 file changed

+31
-0
lines changed
  • include/boost/math/special_functions

1 file changed

+31
-0
lines changed

include/boost/math/special_functions/next.hpp

+31
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ typename tools::promote_args<T, U>::type float_distance(const T& a, const U& b)
725725
inline std::int32_t float_distance(float a, float b)
726726
{
727727
using std::abs;
728+
using std::isfinite;
728729
constexpr auto tol = 2 * (std::numeric_limits<float>::min)();
729730

730731
// 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)
737738
return static_cast<std::int32_t>(float_distance(a, b, policies::policy<>()));
738739
}
739740

741+
static const char* function = "float_distance<%1%>(%1%, %1%)";
742+
if(!(boost::math::isfinite)(a))
743+
{
744+
return policies::raise_domain_error<float>(
745+
function,
746+
"Argument a must be finite, but got %1%", a, policies::policy<>());
747+
}
748+
if(!(boost::math::isfinite)(b))
749+
{
750+
return policies::raise_domain_error<float>(
751+
function,
752+
"Argument b must be finite, but got %1%", b, policies::policy<>());
753+
}
754+
740755
static_assert(sizeof(float) == sizeof(std::int32_t), "float is incorrect size.");
741756

742757
std::int32_t ai;
@@ -757,6 +772,7 @@ inline std::int32_t float_distance(float a, float b)
757772
inline std::int64_t float_distance(double a, double b)
758773
{
759774
using std::abs;
775+
using std::isfinite;
760776
constexpr auto tol = 2 * (std::numeric_limits<double>::min)();
761777

762778
// 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)
768784
{
769785
return static_cast<std::int64_t>(float_distance(a, b, policies::policy<>()));
770786
}
787+
788+
static const char* function = "float_distance<%1%>(%1%, %1%)";
789+
if(!(boost::math::isfinite)(a))
790+
{
791+
return policies::raise_domain_error<double>(
792+
function,
793+
"Argument a must be finite, but got %1%", a, policies::policy<>());
794+
}
795+
if(!(boost::math::isfinite)(b))
796+
{
797+
return policies::raise_domain_error<double>(
798+
function,
799+
"Argument b must be finite, but got %1%", b, policies::policy<>());
800+
}
801+
771802

772803
static_assert(sizeof(double) == sizeof(std::int64_t), "double is incorrect size.");
773804

0 commit comments

Comments
 (0)