Why is u
in the program below always infinity?
#include <random>
#include <limits>
int main()
{
auto seed = std::random_device()();
std::mt19937 randomEngine(seed);
const double lo = std::numeric_limits<double>::lowest(); // ~= -1.8e+308
const double hi = std::numeric_limits<double>::max(); // ~= 1.8e+308
std::uniform_real_distribution<> U(lo, hi);
double u = U(randomEngine); // always 1.#INF000000000000
return 0;
}
It's clearly something to do with the range passed to std::uniform_real_distribution
. If I pass it (lo,0)
or (0,hi)
it generates finite random numbers, but why?
According to N3797:
26.5.8.2.2 [rand.dist.uni.real]
2 Requires:
a ≤ b
andb − a ≤ numeric_limits<RealType>::max()
.
This condition is not met since b - a
is 2 * numeric_limits<double>::max()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With