Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mborland committed Aug 13, 2022
1 parent 793b12d commit 2c2b13a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion include/boost/math/special_functions/next.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#include <boost/math/tools/traits.hpp>
#include <boost/math/tools/config.hpp>
#include <type_traits>
#include <stdexcept>
#include <cfloat>
#include <cstdint>
#include <cstring>


#if !defined(_CRAYC) && !defined(__CUDACC__) && (!defined(__GNUC__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 3)))
#if (defined(_M_IX86_FP) && (_M_IX86_FP >= 2)) || defined(__SSE2__)
#include "xmmintrin.h"
Expand Down Expand Up @@ -725,6 +725,7 @@ typename tools::promote_args<T, U>::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<float>::min)();

// 0, very small, and large magnitude distances all need special handling
Expand All @@ -736,6 +737,10 @@ inline std::int32_t float_distance(float a, float b)
{
return static_cast<std::int32_t>(float_distance(a, b, policies::policy<>()));
}
else if (!(isfinite)(a) || !(isfinite)(b))
{
throw std::domain_error("a and b must both be finite");
}

static_assert(sizeof(float) == sizeof(std::int32_t), "float is incorrect size.");

Expand All @@ -757,6 +762,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<double>::min)();

// 0, very small, and large magnitude distances all need special handling
Expand All @@ -768,6 +774,11 @@ inline std::int64_t float_distance(double a, double b)
{
return static_cast<std::int64_t>(float_distance(a, b, policies::policy<>()));
}
else if (!(isfinite)(a) || !(isfinite)(b))
{
throw std::domain_error("a and b must both be finite");
}


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

Expand Down

0 comments on commit 2c2b13a

Please sign in to comment.