C ++ error in uniform real distribution?

I am trying to use std::uniform_real_distribution<float>(a, b)to generate random floats, and I found a case where the result is equal to the upper limit b. According to: http://www.cplusplus.com/reference/random/uniform_real_distribution http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution this should not happen.

When using gcc-4.9.2and clang-3.5.0for me the following failures:

#include <iomanip>
#include <iostream>
#include <limits>
#include <random>

int main() {
    float a = 1.0f;
    float b = 1.001f;
    size_t seed = 293846;
    size_t n = 9830;
    std::mt19937 rg(seed);
    std::uniform_real_distribution< float > u(a, b);
    for (size_t i = 0; i < n; ++i) {
        float v = u(rg);
        if (not (v < b)) {
            std::cerr << "error: i=" << i
                      << " v=" << std::scientific << std::setprecision(std::numeric_limits< double >::max_digits10) << v
                      << " b=" << b << std::endl;
            abort();
        }
    }
}

The output I see is:

error: i=9829 v=1.00100004673004150e+00 b=1.00100004673004150e+00
Aborted (core dumped)

Is this a bug with the standard library? Did I miss something?

Edit Have . Although this is not an exact duplicate, the question raised in this question is indeed covered by the answer to another question mentioned here.

LLVM : https://llvm.org/bugs/show_bug.cgi?id=18767

+4
1

[rand.dist.uni.real] , - , .

, , , ( ) , .

0

Source: https://habr.com/ru/post/1614513/


All Articles