rampion rampion - 2 months ago 25
C++ Question

boost::unit_test::data::random(-FLT_MAX, FLT_MAX) only generates +Infinity

I'm using

(with
boost-1.61.0_1
installed) and I'm having some issues generating random floats using
boost::unit_test::data::random(-FLT_MAX,FLT_MAX)
. It only seems to generate
+Infinity
.

Through trial and error, I found that I could generate random floats in
[-FLT_MAX,-FLT_MAX * 2^-25)
and
[-FLT_MAX * 2^-25, FLT_MAX)
separately, which gives me a possible work-around, but I'm still curious as to what I'm doing wrong trying to generate floats in
[-FLT_MAX, FLT_MAX)
.

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>
#include <cfloat>

inline void in_range(float const & min, float const & x, float const & max) {
BOOST_TEST_REQUIRE(min <= x);
BOOST_TEST_REQUIRE(x < max);
}

static constexpr float lo{-FLT_MAX / (1024.0 * 1024.0 * 32.0)};

// this test passes
namespace bdata = boost::unit_test::data;
BOOST_DATA_TEST_CASE(low_floats, bdata::random(-FLT_MAX, lo) ^ bdata::xrange(100), x,
index) {
#pragma unused(index)
in_range(-FLT_MAX, x, lo);
}
// this test passes
BOOST_DATA_TEST_CASE(high_floats, bdata::random(lo, FLT_MAX) ^ bdata::xrange(100), x,
index) {
#pragma unused(index)
in_range(lo, x, FLT_MAX);
}
// this test fails
BOOST_DATA_TEST_CASE(all_floats, bdata::random(-FLT_MAX, FLT_MAX) ^ bdata::xrange(100), x,
index) {
#pragma unused(index)
in_range(-FLT_MAX, x, FLT_MAX);
}


results in:

$ ./example
Running 300 test cases...
example.cpp:9: fatal error: in "all_floats": critical check x < max has failed [inf >= 3.40282347e+38]
Failure occurred in a following context:
x = inf; index = 0;
...
example.cpp:9: fatal error: in "all_floats": critical check x < max has failed [inf >= 3.40282347e+38]
Failure occurred in a following context:
x = inf; index = 99;

*** 100 failures are detected in the test module "example"

Answer

boost::unit_test::data::random uses std::uniform_real_distribution, which has the requirement:

Requires: a ≤ b and b - a ≤ numeric_limits<RealType>::max().

In your case, b - a is 2 * FLT_MAX, which is +Inf in float.

You could use your workaround, or you could generate in double and cast back to float.