16
16
#include < boost/math/special_functions/sign.hpp>
17
17
#include < boost/math/special_functions/trunc.hpp>
18
18
#include < boost/math/tools/traits.hpp>
19
+ #include < boost/math/tools/config.hpp>
19
20
#include < type_traits>
20
21
#include < cfloat>
21
22
#include < cstdint>
23
+ #include < cstring>
22
24
23
25
24
26
#if !defined(_CRAYC) && !defined(__CUDACC__) && (!defined(__GNUC__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 3)))
@@ -719,10 +721,11 @@ typename tools::promote_args<T, U>::type float_distance(const T& a, const U& b)
719
721
}
720
722
721
723
// https://randomascii.wordpress.com/2012/01/23/stupid-float-tricks-2/
724
+ // https://blog.regehr.org/archives/959
722
725
inline std::int32_t float_distance (float a, float b)
723
726
{
724
727
using std::abs ;
725
- constexpr float tol = 2 * (std::numeric_limits<float >::min)();
728
+ constexpr auto tol = 2 * (std::numeric_limits<float >::min)();
726
729
727
730
// 0, very small, and large magnitude distances all need special handling
728
731
if (abs (a) == 0 || abs (b) == 0 )
@@ -736,8 +739,10 @@ inline std::int32_t float_distance(float a, float b)
736
739
737
740
static_assert (sizeof (float ) == sizeof (std::int32_t ), " float is incorrect size." );
738
741
739
- const auto ai = *reinterpret_cast <std::int32_t *>(&a);
740
- const auto bi = *reinterpret_cast <std::int32_t *>(&b);
742
+ std::int32_t ai;
743
+ std::int32_t bi;
744
+ std::memcpy (&ai, &a, sizeof (float ));
745
+ std::memcpy (&bi, &b, sizeof (float ));
741
746
auto result = bi - ai;
742
747
743
748
if (ai < 0 || bi < 0 )
0 commit comments