Skip to content

Commit 793b12d

Browse files
committed
Add specialized float distance for type double
1 parent 03acbaa commit 793b12d

File tree

1 file changed

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

1 file changed

+33
-0
lines changed

include/boost/math/special_functions/next.hpp

+33
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,39 @@ inline std::int32_t float_distance(float a, float b)
743743
std::int32_t bi;
744744
std::memcpy(&ai, &a, sizeof(float));
745745
std::memcpy(&bi, &b, sizeof(float));
746+
747+
auto result = bi - ai;
748+
749+
if (ai < 0 || bi < 0)
750+
{
751+
result = -result;
752+
}
753+
754+
return result;
755+
}
756+
757+
inline std::int64_t float_distance(double a, double b)
758+
{
759+
using std::abs;
760+
constexpr auto tol = 2 * (std::numeric_limits<double>::min)();
761+
762+
// 0, very small, and large magnitude distances all need special handling
763+
if (abs(a) == 0 || abs(b) == 0)
764+
{
765+
return static_cast<std::int64_t>(float_distance(a, b, policies::policy<>()));
766+
}
767+
else if (abs(a) < tol || abs(b) < tol)
768+
{
769+
return static_cast<std::int64_t>(float_distance(a, b, policies::policy<>()));
770+
}
771+
772+
static_assert(sizeof(double) == sizeof(std::int64_t), "double is incorrect size.");
773+
774+
std::int64_t ai;
775+
std::int64_t bi;
776+
std::memcpy(&ai, &a, sizeof(double));
777+
std::memcpy(&bi, &b, sizeof(double));
778+
746779
auto result = bi - ai;
747780

748781
if (ai < 0 || bi < 0)

0 commit comments

Comments
 (0)